-
-
Save kevsersrca/5e67e7b1795ddf38774dbe5d4eabc576 to your computer and use it in GitHub Desktop.
Error loging decorator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment