August 2018
Intermediate to advanced
366 pages
10h 14m
English
The logging module has a way to report exceptions by email, so we can set up a logger and trap the exceptions to log them by email:
import logging import logging.handlers import functools crashlogger = logging.getLogger('__crashes__') def configure_crashreport(mailhost, fromaddr, toaddrs, subject, credentials, tls=False): if configure_crashreport._configured: return crashlogger.addHandler( logging.handlers.SMTPHandler( mailhost=mailhost, fromaddr=fromaddr, toaddrs=toaddrs, subject=subject, credentials=credentials, secure=tuple() if tls else None ) ) configure_crashreport._configured = True configure_crashreport._configured = False def crashreport(f): @functools.wraps(f) def _crashreport(*args, **kwargs): try: return f(*args, ...