While you should use a proper crash reporter like our sponsor Sentry in production, structlog has helpers for formatting exceptions for humans and machines.
All structog’s exception features center around passing an
exc_info key-value pair in the event dict.
There are three possible behaviors depending on its value:
If the value is a tuple, render it as if it was returned by
If the value is an Exception, render it.
If the value is true but no tuple, call
sys.exc_info()and render that.
If there is no
exc_info key or false, the event dict is not touched.
This behavior is analog to the one of the stdlib’s logging.
structlog comes with
structlog.processors.ExceptionRenderer that deduces and removes the
exc_info key as outlined above, calls a user-supplied function with the synthesized
exc_info, and stores its return value in the
The most common use-cases are already covered by the following processors:
Uses better-exceptions to render a colorful traceback.
It’s the default if better-exceptions is installed and Rich is not.
Uses Rich to render a colorful traceback. It’s a class because it allows for customizing the output by passing arguments to Rich.
It’s the default if Rich is installed.
Console Output for more information on structlog’s console features.