Search code examples
pythonstructlog

Async structlog configuration


In the structlog documentation https://www.structlog.org/en/stable/performance.html is an example for a sync structlog configuration:

import logging
import structlog

structlog.configure(
    cache_logger_on_first_use=True,
    wrapper_class=structlog.make_filtering_bound_logger(logging.INFO),
    processors=[
        structlog.threadlocal.merge_threadlocal,
        structlog.processors.add_log_level,
        structlog.processors.format_exc_info,
        structlog.processors.TimeStamper(fmt="iso", utc=True),
        structlog.processors.JSONRenderer(serializer=orjson.dumps),
    ],
    logger_factory=structlog.BytesLoggerFactory(),
)

What is the equivalent async configuration?


Solution

  • EDIT: as of structlog 22.2.0, FilteringBoundLogger had a full set of async methods that prefix the regular ones with an a like ainfo.

    It’s a different approach but I think it’s the better one and it might find its way back into stdlib.


    Unfortunately, async is currently stdlib-only. The only reason is that nobody has done it yet.

    The ticket to track that includes an outline of a workaround: https://github.com/hynek/structlog/issues/354~