August 2018
Intermediate to advanced
366 pages
10h 14m
English
The logging module is able to send messages to any handler attached to logger, and has a feature to explicitly log crashes by logging an exception and its traceback through .exception.
So the root of our solution to send exceptions by email is to wrap the main function of our code base with a decorator that traps all exceptions and invokes the logger:
def crashreport(f):
@functools.wraps(f)
def _crashreport(*args, **kwargs):
try:
return f(*args, **kwargs)
except Exception as e:
crashlogger.exception(
'{} crashed\n'.format(f.__name__)
)
raise
return _crashreport
The crashlogger.exception method will build a message that contains our custom text (which reports the name of the decorated function) plus the traceback for the crash, ...