explaingit

hynek/structlog

4,780PythonAudience · developerComplexity · 2/5Setup · easy

TLDR

A Python logging library that attaches named fields to every log message instead of writing plain text lines, making logs easy to search and process with external tools, supports JSON, logfmt, and colored console output through a configurable processing pipeline.

Mindmap

mindmap
  root((structlog))
    What it does
      Structured logging
      Named field events
      Pipeline processing
    Output formats
      JSON renderer
      Colored console
      logfmt format
    Use cases
      Web app logging
      Async programs
      Log aggregation
    Integration
      Python logging
      asyncio support
      PyPI install
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Replace Python built-in logging with structured JSON output that tools like Datadog or Elasticsearch can parse and filter by field

USE CASE 2

Automatically attach request-level context such as user ID and request ID to every log line within a web application

USE CASE 3

Switch between colored console logs for development and JSON logs for production with a single configuration change

USE CASE 4

Add structlog alongside an existing Python logging setup without replacing the current logging infrastructure

Tech stack

PythonPyPIasyncioJSONlogfmt

Getting it running

Difficulty · easy Time to first run · 5min

No external dependencies required, works alongside Python built-in logging module and is available via pip install structlog.

No license information was provided in the explanation.

In plain English

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.

Copy-paste prompts

Prompt 1
I have a FastAPI app and want structlog to automatically include the request ID and user ID on every log line as JSON. Show me the setup code including the middleware and processor chain.
Prompt 2
How do I configure structlog to output colored logs during local development and switch to JSON output in production using an environment variable?
Prompt 3
I already use Python built-in logging throughout my codebase and want to adopt structlog incrementally without rewriting everything. Show me how to make them work together.
Prompt 4
Write a custom structlog processor that automatically adds the current hostname and service name to every log event in my Python service.
Open on GitHub → Explain another repo

← hynek on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.