← Back to all products
$19
Python Logging & Config
Structured logging with structlog, environment-based configuration, secrets management, and 12-factor app patterns.
PythonYAMLMarkdownJSONFastAPIDjangoFlask
📁 File Structure 16 files
python-logging-config/
├── LICENSE
├── README.md
├── configs/
│ ├── logging_dev.yaml
│ ├── logging_prod.yaml
│ └── logging_test.yaml
├── examples/
│ ├── celery_example.py
│ └── fastapi_example.py
├── guides/
│ └── logging-guide.md
├── src/
│ ├── context.py
│ ├── filters.py
│ ├── formatters.py
│ ├── handlers.py
│ ├── middleware.py
│ └── setup.py
└── tests/
└── test_logging.py
📖 Documentation Preview README excerpt
Python Logging Config — Production-Ready Logging Setup
Structured logging, request context, ASGI/WSGI middleware, and environment-specific configs in one drop-in package.
What You Get
- One-call setup —
configure_logging("prod")loads the right YAML config - Structured JSON formatter — machine-readable logs for Datadog, ELK, CloudWatch
- Request context — automatic
request_id,user_idin every log line - ASGI & WSGI middleware — plug into FastAPI, Starlette, Flask, Django
- Smart filters — suppress noisy loggers, rate-limit repeated messages
- 3 YAML configs — dev (colorized console), prod (JSON to file + stdout), test (minimal)
File Tree
python-logging-config/
├── README.md
├── manifest.json
├── LICENSE
├── src/
│ ├── setup.py # One-call logging configuration
│ ├── formatters.py # JSON, colored, and key-value formatters
│ ├── handlers.py # Rotating file, async queue handler
│ ├── context.py # Context variables (request_id, user_id)
│ ├── middleware.py # ASGI and WSGI middleware
│ └── filters.py # Rate-limit and suppression filters
├── configs/
│ ├── logging_dev.yaml # Development config
│ ├── logging_prod.yaml # Production config
│ └── logging_test.yaml # Test config
├── examples/
│ ├── fastapi_example.py # FastAPI integration
│ └── celery_example.py # Celery task logging
├── tests/
│ └── test_logging.py
└── guides/
└── logging-guide.md
Getting Started
Quick setup
from src.setup import configure_logging
configure_logging("dev") # Colorized console output
import logging
logger = logging.getLogger(__name__)
logger.info("Application started")
Structured JSON logging (production)
configure_logging("prod")
logger.info("Order placed", extra={"order_id": "ORD-123", "total": 49.99})
# {"timestamp": "2026-03-10T12:00:00Z", "level": "INFO", "message": "Order placed", "order_id": "ORD-123", ...}
*... continues with setup instructions, usage examples, and more.*
📄 Code Sample .py preview
src/context.py
"""Logging context variables — propagate request_id, user_id, etc.
Uses :mod:`contextvars` so each async task / thread gets its own context
automatically.
Usage:
from src.context import set_context, get_context
set_context(request_id="req-abc-123", user_id="usr-42")
# ... later, in any log formatter:
ctx = get_context() # {"request_id": "req-abc-123", "user_id": "usr-42"}
"""
from __future__ import annotations
import contextvars
import uuid
from typing import Any
_request_id: contextvars.ContextVar[str | None] = contextvars.ContextVar(
"request_id", default=None
)
_user_id: contextvars.ContextVar[str | None] = contextvars.ContextVar(
"user_id", default=None
)
_correlation_id: contextvars.ContextVar[str | None] = contextvars.ContextVar(
"correlation_id", default=None
)
_extra: contextvars.ContextVar[dict[str, Any]] = contextvars.ContextVar(
"log_extra", default={}
)
def set_context(
*,
request_id: str | None = None,
user_id: str | None = None,
correlation_id: str | None = None,
**kwargs: Any,
) -> None:
"""Set context variables for the current execution context.
Args:
request_id: Unique ID for the current request.
user_id: Authenticated user identifier.
correlation_id: Cross-service correlation ID.
**kwargs: Additional key-value pairs added to context.
"""
if request_id is not None:
_request_id.set(request_id)
# ... 50 more lines ...