Structured Logging with structlog
Configure structlog for JSON-formatted structured logging with request context, timestamps, and log levels.
import structlog
import logging
import sys
def setup_logging(json_output: bool = True, level: str = "INFO") -> None:
"""Configure structlog with processors and stdlib integration."""
shared_processors: list[structlog.types.Processor] = [
structlog.contextvars.merge_contextvars,
structlog.stdlib.add_log_level,
structlog.stdlib.add_logger_name,
structlog.processors.TimeStamper(fmt="iso"),
structlog.processors.StackInfoRenderer(),
structlog.processors.UnicodeDecoder(),
]
if json_output:
renderer = structlog.processors.JSONRenderer()
else:
renderer = structlog.dev.ConsoleRenderer()
structlog.configure(
processors=[
*shared_processors,
structlog.stdlib.ProcessorFormatter.wrap_for_formatter,
],
logger_factory=structlog.stdlib.LoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
cache_logger_on_first_use=True,
)
formatter = structlog.stdlib.ProcessorFormatter(
processors=[*shared_processors, renderer]
)
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(formatter)
root = logging.getLogger()
root.handlers.clear()
root.addHandler(handler)
root.setLevel(level)
# Usage:
# setup_logging(json_output=True)
# log = structlog.get_logger()
#
# log.info("user_login", user_id=42, ip="1.2.3.4")
# Output: {"event": "user_login", "user_id": 42, "ip": "1.2.3.4", ...}
#
# # Bind context for request lifecycle
# structlog.contextvars.bind_contextvars(request_id="abc-123")
# log.info("processing") # includes request_id automaticallySponsored
Datadog — Cloud monitoring and logging
Use Cases
- Application logging
- Request tracing
- Production observability
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
Structured JSON Logger
Build a structured logger with log levels, context, child loggers, and JSON output for Node.js services.
Best for: Application logging for production
Structured Request Logger Middleware
Express middleware that logs request/response details as structured JSON with timing information.
Best for: API monitoring
Structured Logging for Data Pipelines
Use Loguru to emit structured JSON logs with contextual fields from ETL pipeline stages.
Best for: pipeline observability
Java Logging with SLF4J and Logback
Configure structured logging with SLF4J: log levels, MDC context, JSON format, and best practices.
Best for: Application logging with contextual information