Replace Python built-in logging with structured JSON output that tools like Datadog or Elasticsearch can parse and filter by field
Automatically attach request-level context such as user ID and request ID to every log line within a web application
Switch between colored console logs for development and JSON logs for production with a single configuration change
Add structlog alongside an existing Python logging setup without replacing the current logging infrastructure
No external dependencies required, works alongside Python built-in logging module and is available via pip install structlog.
Structlog is a logging library for Python that focuses on attaching structured data to log messages rather than writing plain text lines. When a program records what it is doing, traditional logging produces lines like "user logged in" -- useful for humans reading a file, but hard to search and analyze automatically. Structlog instead produces log entries with named fields, such as user ID, request duration, and status code all attached to the same event. This makes logs much easier to process with external tools that can filter, count, and alert on specific fields. The library is built around a pipeline concept. Each log call passes through a series of processing functions that can add context, filter messages, or transform the data before it gets written anywhere. You can chain these functions in any order you choose. This design means structlog can work alongside Python's built-in logging module, sending its output there if you already have tooling set up around that, or it can write directly to a file, console, or external service on its own. The output format is configurable. Structlog ships with a colored console renderer for development, a JSON renderer for production systems where logs get ingested by aggregation tools, and a logfmt renderer, which is a compact key-value format common in infrastructure tooling. Switching between formats is a configuration change, not a code change. The library has been in production use since 2013 and supports modern Python features including asyncio for asynchronous programs and type hints for code editors that can check types. It is available through PyPI, Python's package repository, and has detailed documentation with tutorials for getting started. The project is maintained by a single author supported by sponsors and a commercial support subscription available through Tidelift.
← hynek on gitmyhub — every repo by this author, as a profile.
Verify against the repo before relying on details.