Spaces:
Sleeping
Sleeping
File size: 1,337 Bytes
71c1ad2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | # app/observability/logging.py
# Structured logging with structlog
import sys
import logging
import structlog
from app.config import get_settings
def setup_logging() -> None:
"""Configure structured logging for the application."""
settings = get_settings()
# Configure structlog
structlog.configure(
processors=[
structlog.contextvars.merge_contextvars,
structlog.processors.add_log_level,
structlog.processors.StackInfoRenderer(),
structlog.dev.set_exc_info,
structlog.processors.TimeStamper(fmt="iso"),
structlog.processors.JSONRenderer()
if settings.is_production
else structlog.dev.ConsoleRenderer(colors=True),
],
wrapper_class=structlog.make_filtering_bound_logger(
logging.getLevelName(settings.log_level)
),
context_class=dict,
logger_factory=structlog.PrintLoggerFactory(),
cache_logger_on_first_use=True,
)
# Silence noisy third-party loggers
for logger_name in ["uvicorn.access", "httpx", "httpcore"]:
logging.getLogger(logger_name).setLevel(logging.WARNING)
def get_logger(name: str | None = None) -> structlog.BoundLogger:
"""Get a structured logger instance."""
return structlog.get_logger(name or __name__)
|