
Python Logging & Config: Python Logging Guide
Python Logging Guide Why Structured Logging? Plain-text logs are hard to search, filter, and alert on at scale. Structured logging (JSON) makes every log line a queryable record — essential for production observability. Feature Plain Text Structured (JSON) Human-readable Yes With tooling Machine-parseable Regex needed Native Searchable grep Field queries Alerting Pattern match Exact field match Context (request_id) Manual Automatic Configuration Strategy Use environment-specific YAML configs: configs/ ├── logging_dev.yaml # Colorized console, DEBUG level ├── logging_prod.yaml # JSON to stdout + file, INFO level └── logging_test.yaml # WARNING only, minimal output Load with one call: from src.setup import configure_logging configure_logging ( " prod " ) Request Context Propagation Every log line should identify which request it belongs to. This toolkit uses contextvars (works with asyncio, threads, and sync code): from src.context import set_context , generate_request_id # In middleware
Continue reading on Dev.to Python
Opens in a new tab



