Structured JSON Logging with structlog
How to emit structured JSON logs in Python using structlog, including context binding, log levels, processors, and integration with standard logging.
Overview
structlog produces structured logs — each log entry is a dictionary with keys for timestamp, level, event, and any context you bind. Unlike plain print() or logging.getLogger(), structlog outputs JSON that log aggregators (ELK, Loki, Datadog) can parse without regex. You bind request-scoped context (user ID, trace ID) once, and every subsequent log line includes it automatically.
When to Use
- Applications that ship logs to a log aggregator (ELK, Loki, Splunk, Datadog)
- Microservices where you need to trace a request across service boundaries via correlation IDs
- APIs where you want to log request method, path, status code, and duration in every line
- Replacing
print()debugging with structured, filterable log output
When NOT to Use
- Simple scripts where
print()is sufficient — structlog adds a dependency and config - CLI tools that output human-readable text — use
rich.loggingorclick.echoinstead - High-throughput hot paths where logging itself is the bottleneck — sample or batch instead
Solution
Setup
pip install structlog
Basic structured logging
import structlog
logger = structlog.get_logger()
logger.info("user_logged_in", user_id=42, method="oauth")
logger.warning("rate_limit_approaching", user_id=42, remaining=5)
logger.error("payment_failed", order_id="ord_123", reason="card_declined")
Output (JSON):
{"event": "user_logged_in", "user_id": 42, "method": "oauth", "level": "info",
"timestamp": "2026-07-05T10:30:00Z"}
Configuration with processors
import structlog
import logging
structlog.configure(
processors=[
structlog.contextvars.merge_contextvars,
structlog.processors.add_log_level,
structlog.processors.TimeStamper(fmt="iso"),
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
structlog.processors.JSONRenderer(),
],
wrapper_class=structlog.make_filtering_bound_logger(logging.INFO),
logger_factory=structlog.PrintLoggerFactory(),
cache_logger_on_first_use=True,
)
Context binding
logger = structlog.get_logger()
# Bind context that persists across all log calls
request_logger = logger.bind(request_id="req-abc-123", user_id=42)
request_logger.info("processing_order", order_id="ord_456")
request_logger.info("order_validated", items=3)
request_logger.warning("inventory_low", sku="widget-001", stock=2)
Every log line includes request_id and user_id automatically.
Using contextvars for async context
import structlog
from contextvars import ContextVar
request_id_var: ContextVar[str] = ContextVar("request_id", default="")
def set_request_context(request_id: str, user_id: int):
structlog.contextvars.bind_contextvars(
request_id=request_id,
user_id=user_id,
)
def clear_request_context():
structlog.contextvars.clear_contextvars()
# In an ASGI middleware
async def logging_middleware(request, call_next):
request_id = request.headers.get("X-Request-ID", str(uuid.uuid4()))
set_request_context(request_id, user_id=extract_user_id(request))
try:
response = await call_next(request)
logger.info("request_completed", status=response.status_code, path=request.url.path)
return response
finally:
clear_request_context()
Integration with standard logging
import logging
import structlog
# Configure structlog
structlog.configure(
processors=[
structlog.processors.add_log_level,
structlog.processors.TimeStamper(fmt="iso"),
structlog.processors.JSONRenderer(),
],
)
# Configure standard logging to route through structlog
logging.basicConfig(
format="%(message)s",
stream=sys.stdout,
level=logging.INFO,
)
# Third-party libraries using standard logging will also output JSON
logging.getLogger("urllib3").info("Connection pool created")
Log levels and filtering
import structlog
import logging
structlog.configure(
processors=[
structlog.processors.add_log_level,
structlog.processors.TimeStamper(fmt="iso"),
structlog.processors.JSONRenderer(),
],
wrapper_class=structlog.make_filtering_bound_logger(logging.WARNING),
)
logger = structlog.get_logger()
logger.info("this_is_filtered") # Not emitted (below WARNING)
logger.warning("this_shows_up", key="value") # Emitted
Exception logging with traceback
logger = structlog.get_logger()
try:
result = 1 / 0
except ZeroDivisionError:
logger.exception("division_failed", operation="calculate_ratio")
Output includes the full traceback in the exception field.
Custom processor for sensitive data redaction
def redact_sensitive_data(logger, method_name, event_dict):
sensitive_keys = {"password", "api_key", "token", "credit_card"}
for key in list(event_dict.keys()):
if key.lower() in sensitive_keys:
event_dict[key] = "[REDACTED]"
return event_dict
structlog.configure(
processors=[
redact_sensitive_data,
structlog.processors.add_log_level,
structlog.processors.TimeStamper(fmt="iso"),
structlog.processors.JSONRenderer(),
],
)
logger = structlog.get_logger()
logger.info("user_login", email="alice@example.com", password="secret123")
# Output: {"event": "user_login", "email": "alice@example.com", "password": "[REDACTED]", ...}
Performance timing processor
import time
def add_timing(logger, method_name, event_dict):
if "start_time" in event_dict:
event_dict["duration_ms"] = round((time.time() - event_dict.pop("start_time")) * 1000, 2)
return event_dict
structlog.configure(
processors=[
add_timing,
structlog.processors.add_log_level,
structlog.processors.TimeStamper(fmt="iso"),
structlog.processors.JSONRenderer(),
],
)
logger = structlog.get_logger()
start = time.time()
# ... do work ...
logger.info("database_query", start_time=start, query="SELECT * FROM users")
How It Works
Each logger.info(...) call builds an event_dict that travels through the processor chain left to right. Every processor receives the dict, mutates or enriches it, and passes it along — merge_contextvars injects the request-scoped keys, add_log_level and TimeStamper stamp metadata, your custom processors redact or time, and the last one (usually JSONRenderer) serializes everything to a single JSON line. The make_filtering_bound_logger wrapper drops below-threshold calls before any of that work happens, which is why filtered logs cost almost nothing.
Variants
Using structlog with FastAPI
from fastapi import FastAPI, Request
import structlog
import uuid
app = FastAPI()
logger = structlog.get_logger()
@app.middleware("http")
async def log_requests(request: Request, call_next):
request_id = str(uuid.uuid4())
structlog.contextvars.bind_contextvars(
request_id=request_id,
method=request.method,
path=request.url.path,
)
response = await call_next(request)
logger.info("request_completed", status_code=response.status_code)
structlog.contextvars.clear_contextvars()
return response
Using structlog with Celery
from celery import Celery
import structlog
app = Celery("tasks", broker="redis://localhost:6379")
logger = structlog.get_logger()
@app.task
def process_order(order_id: str):
logger.bind(task_id=app.current_task.request.id, order_id=order_id)
logger.info("processing_started")
# ... process ...
logger.info("processing_completed")
Best Practices
-
A runnable version with the processor chain and the FastAPI middleware lives in the companion repo.
-
For the same pattern in Node.js, see High-Performance Logging with pino.
-
For metrics on top of these logs, see Custom Prometheus Metrics in Python; for distributed traces, see OpenTelemetry Tracing in Python.
-
Use
contextvarsfor request-scoped context in async frameworks — it propagates correctly acrossawaitboundaries -
Always include a
timestampprocessor — log aggregators need it for ordering -
Add a
request_idortrace_idto every log line for distributed tracing correlation -
Use
logger.exception()(notlogger.error()) in exception handlers — it includes the traceback -
Configure structlog once at application startup, not per module
-
Use
cache_logger_on_first_use=Truefor performance in hot paths -
Redact sensitive fields (passwords, tokens, PII) with a custom processor
Common Mistakes
- Binding context on the root logger:
logger.bind()returns a new logger — it doesn’t mutate the root. Store the bound logger in a variable. - Not clearing contextvars: in async frameworks the context survives into the next request handled by the same task. Clear it in a
finallyblock or the second request inherits the first one’s IDs. - Using string interpolation instead of kwargs:
logger.info(f"User {user_id} logged in")flattens the value into the message — you can’t filter on it later. Uselogger.info("user_logged_in", user_id=user_id). - Not configuring standard logging: third-party libraries (urllib3, boto3) use
logging. Without integration, their output is unstructured. - Logging at INFO level in hot loops: a 10,000-iteration loop with one INFO log per pass writes 10,000 lines. Drop to DEBUG or sample every Nth call.
Further Reading
- structlog documentation — processors, contextvars, and stdlib integration
- Python logging cookbook — official stdlib patterns that pair with structlog
- The Twelve-Factor App — Logs — why apps should treat logs as event streams on stdout
Frequently Asked Questions
How do I output human-readable logs in development?
Swap JSONRenderer() for structlog.dev.ConsoleRenderer() in the processor list — you get colored, formatted output locally without touching anything else. Keep the JSON renderer behind an environment flag for production.
Can I use structlog with Django?
Yes. Point Django's LOGGING dict at a StreamHandler and configure structlog with structlog.stdlib.LoggerFactory() so both worlds route through the same handler.
How do I add a correlation ID across microservices?
Generate a UUID at the entry point, forward it in an HTTP header like X-Correlation-ID, and in the receiving service bind it with structlog.contextvars.bind_contextvars(correlation_id=corr_id) before handling the request.
What is the performance overhead of structlog?
Below-threshold calls are no-ops when you use make_filtering_bound_logger, and the JSON serialization only runs for lines that pass the filter. In practice a passing log call costs well under a millisecond.
How do I ship structlog output to Loki or ELK?
Write the JSON lines to stdout and let a collector (Fluent Bit, Filebeat, Promtail) tail them — since every line is already a JSON document, the collector needs no grok or parsing rules.
Should I log to stdout or to a file?
stdout in containers and systemd services — the platform already routes it to the collector. Files only make sense for long-running bare-metal processes, and even then a rotating handler plus a tailer beats parsing application-side.
Related Resources
High-Performance Logging with pino
How to use pino for fast structured JSON logging in Node.js, including log levels, child loggers, transports, and integration with Express and Fastify.
RecipeExpose Custom Application Metrics with Python and Prometheus
Build a custom Prometheus metrics exporter in Python using prometheus_client. Covers counters, gauges, histograms, summaries, and Flask/FastAPI integration.
RecipeDistributed Tracing with OpenTelemetry
How to implement distributed tracing in Python with OpenTelemetry SDK, including spans, context propagation, auto-instrumentation, and Jaeger export.
RecipeStructured Logging: JSON Logs with Correlation IDs
Implement structured logging with JSON output, correlation IDs, and log aggregation for production observability.
RecipeError Tracking with Sentry in Express
How to integrate Sentry for error tracking in Node.js Express apps with the v8+ SDK — error handlers, performance spans, release tracking and source maps.
RecipeRotate Logs Daily with Winston
How to configure daily log rotation in Node.js using winston and winston-daily-rotate-file: size limits, retention, compression, and multiple transports.