pythonintermediate
Python Logging Configuration
Configure structured logging with handlers, formatters, rotation, and JSON output for production.
pythonPress ⌘/Ctrl + Shift + C to copy
import logging
import logging.handlers
import json
from datetime import datetime, timezone
# JSON formatter for structured logging
class JSONFormatter(logging.Formatter):
def format(self, record: logging.LogRecord) -> str:
log_data = {
"timestamp": datetime.now(timezone.utc).isoformat(),
"level": record.levelname,
"logger": record.name,
"message": record.getMessage(),
"module": record.module,
"function": record.funcName,
"line": record.lineno,
}
if record.exc_info:
log_data["exception"] = self.formatException(record.exc_info)
if hasattr(record, "extra_data"):
log_data["data"] = record.extra_data
return json.dumps(log_data)
def setup_logging(level: str = "INFO", log_file: str = "app.log") -> None:
root = logging.getLogger()
root.setLevel(getattr(logging, level))
# Console handler (human-readable)
console = logging.StreamHandler()
console.setFormatter(logging.Formatter(
"%(asctime)s [%(levelname)s] %(name)s: %(message)s",
datefmt="%H:%M:%S",
))
root.addHandler(console)
# File handler (JSON, rotating)
file_handler = logging.handlers.RotatingFileHandler(
log_file, maxBytes=10_000_000, backupCount=5
)
file_handler.setFormatter(JSONFormatter())
root.addHandler(file_handler)
# Usage
setup_logging("DEBUG")
logger = logging.getLogger("myapp.orders")
logger.info("Order created", extra={"extra_data": {"order_id": "123", "amount": 99.99}})
logger.warning("Low inventory for product %s", "SKU-456")
try:
1 / 0
except ZeroDivisionError:
logger.exception("Unexpected error processing order")Use Cases
- Production-ready structured logging
- Log aggregation with JSON format
- Rotating file logs for long-running services
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
pythonintermediate
Logging Config
Advanced Python pattern: logging-config
Best for: advanced programming
#python#advanced
typescriptintermediate
Health, Readiness & Liveness Checks
Express routes implementing Kubernetes-style health, readiness, and liveness probe endpoints.
Best for: Kubernetes deployments
#health-check#kubernetes
pythonbeginner
Dataclass with Validation
Python dataclass with __post_init__ field validation, type coercion, and descriptive error messages.
Best for: Data transfer objects
#dataclass#validation
pythonintermediate
Custom Context Manager
Context managers for resource management using both class-based and decorator approaches with error handling.
Best for: Database connection management
#context-manager#resource-management