eliot
eliot copied to clipboard
Logging tracebacks more easily
Currently I need to explicitly include a traceback whenever I want one, but since tracebacks are so useful, I usually do want one. Does it make sense to make this easy by wrapping up the exception, some exception data, and a traceback all together in a way that makes it easy for users to enable?
import inspect
import traceback
import eliot
def _exception_lines(exc: BaseException):
return traceback.format_exception(type(exc), exc, exc.__traceback__)
def _exception_data(exc: BaseException):
# Exclude the attributes that appear on a regular exception,
# aside from a few interesting ones.
exclude = set(dir(Exception())) - {"args", "__cause__", "__context__"}
return {k: v for k, v in inspect.getmembers(exc) if k not in exclude}
def summarize_exception(exc: BaseException):
return {
"exception_lines": _exception_lines(exc),
"exception_data": _exception_data(exc),
}
eliot.register_exception_extractor(Exception, summarize_exception)
Neat. Seems like a good thing to add once I drop Python 2 support, yeah.
Maybe even pull out some values from the exception's frame locals like https://github.com/cknd/stackprinter and http://sentry.io do? Maybe that's too much.