itamarst / eliot

Eliot: the logging system that tells you *why* it happened
https://eliot.readthedocs.io
Apache License 2.0
1.1k stars 66 forks source link

Logging tracebacks more easily #394

Open jtrakk opened 5 years ago

jtrakk commented 5 years ago

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)
itamarst commented 5 years ago

Neat. Seems like a good thing to add once I drop Python 2 support, yeah.

jtrakk commented 5 years ago

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.