22.1.0
Highlights
This is a (too) big release, so it has many highlights!
Firstly, rendering exceptions in machine-readable logs (usually JSON) got a big upgrade: thanks to structlog.processors.dict_tracebacks
you can now have fully structured exceptions in your logs!
To ease getting started with structlog
, we're now shipping structlog.stdlib.recreate_defaults()
that recreates structlog
's default behavior, but on top of standard library's logging
. The output looks the same, but it runs through logging
's machinery and integrates itself easier. The default configuration now also merges your contextvars
-based context, so enjoy structlog.contextvars.bind_contextvars()
without configuring anything!
Another request wish that kept coming up is naming the message key differently than event
. We're aware that nowadays keys like msg
are more common, but structlog
pre-dates the software that introduced and popularized it. To allow for more consistency across your platforms, structlog
now ships structlog.processors.EventRenamer
that allows you to rename the default event
key to something else and additionally also allows you to rename another key to event
.
❤️ Huge thanks to my GitHub sponsors, Tidelift subscribers, and Ko-fi buyers! ❤️
None of my projects would exist in their current form without you!
Full Changelog
Removed
- Python 3.6 is not supported anymore.
- Pickling is now only possible with protocol version 3 and newer.
Deprecated
-
The entire
structlog.threadlocal
module is deprecated. Please use the primitives fromstructlog.contextvars
instead.If you're using the modern APIs (
bind_threadlocal()
/merge_threadlocal()
) it's enough to replace them 1:1 with theircontextvars
counterparts. The old approach aroundwrap_dict()
has been discouraged for a while.Currently there are no concrete plans to remove the module, but no patches against it will be accepted from now on. #409
Added
structlog.processors.StackInfoRenderer
now has an additional_ignores parameter that allows you to filter out your own logging layer. #396- Added
structlog.WriteLogger
, a faster – but more low-level – alternative tostructlog.PrintLogger
. It works the wayPrintLogger
used to work in previous versions. #403 #404 structlog.make_filtering_bound_logger()
-returned loggers now also have alog()
method to match thestructlog.stdlib.BoundLogger
signature closer. #413- Added structured logging of tracebacks via the
structlog.tracebacks
module, and most notably thestructlog.tracebacks.ExceptionDictTransformer
which can be used with the newstructlog.processors.ExceptionRenderer
to render JSON tracebacks. #407 structlog.stdlib.recreate_defaults(log_level=logging.NOTSET)
that recreatesstructlog
's defaults on top of standard library'slogging
. It optionally also configureslogging
to log to standard out at the passed log level. #428structlog.processors.EventRenamer
allows you to rename the hitherto hard-coded event dict keyevent
to something else. Optionally, you can rename another key toevent
at the same time, too. So addingEventRenamer(to="msg", replace_by="_event")
to your processor pipeline will rename the standardevent
key tomsg
and then rename the_event
key toevent
. This allows you to use theevent
key in your own log files and to have consistent log message keys across languages.structlog.dev.ConsoleRenderer(event_key="event")
now allows to customize the name of the key that is used for the log message.
Changed
structlog.make_filtering_bound_logger()
now returns a method with the same signature for all log levels, whether they are active or not. This ensures that invalid calls to inactive log levels are caught immediately and don't explode once the log level changes. #401structlog.PrintLogger
– that is used by default – now usesprint()
for printing, making it a better citizen for interactive terminal applications. #399structlog.testing.capture_logs
now works for already initialized bound loggers. #408structlog.processors.format_exc_info()
is no longer a function, but an instance ofstructlog.processors.ExceptionRenderer
. Its behavior has not changed. #407- The default configuration now includes the
structlog.contextvars.merge_contextvars
processor. That means you can usestructlog.contextvars
features without configuringstructlog
.
Fixed
- Overloaded the
bind
,unbind
,try_unbind
andnew
methods in theFilteringBoundLogger
Protocol. This makes it easier to use objects of typeFilteringBoundLogger
in a typed context. #392 - Monkeypatched
sys.stdout
s are now handled more gracefully byConsoleRenderer
(that's used by default). #404 structlog.stdlib.render_to_log_kwargs()
now correctly handles the presence ofexc_info
,stack_info
, andstackLevel
in the event dictionary. They are transformed into proper keyword arguments instead of putting them into theextra
dictionary. #424, #427