Skip to content

Instantly share code, notes, and snippets.

@alchemyst
Created September 23, 2018 12:05
Show Gist options
  • Select an option

  • Save alchemyst/0f6d62017cd150bc489db7059ba6427f to your computer and use it in GitHub Desktop.

Select an option

Save alchemyst/0f6d62017cd150bc489db7059ba6427f to your computer and use it in GitHub Desktop.

Revisions

  1. alchemyst created this gist Sep 23, 2018.
    34 changes: 34 additions & 0 deletions error_logging.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    import logging
    import sys
    import functools

    logging.basicConfig(level=logging.DEBUG)

    LOGGER = logging

    def log_if_exception(message):
    def decorator(function):
    @functools.wraps(function)
    def exceptionwrapper(*args, **kwargs):
    try:
    return function(*args, **kwargs)
    except BaseException as e:
    LOGGER.critical(message,
    stack_info=True,
    extra={'exception': e,
    'called_function': function.__name__})
    sys.exit(1)
    return exceptionwrapper
    return decorator


    @log_if_exception("Couldn't open file")
    def open_the_file(filename):
    return open(filename)

    @log_if_exception("File does not contain valuation_date")
    def read_value(a):
    return a.read_value("valuation_date")

    a = open_the_file("test.txt")
    b = read_value(a)