43 lines
1.3 KiB
Python
43 lines
1.3 KiB
Python
"""Logging configuration with structured logging."""
|
|
import logging
|
|
import sys
|
|
|
|
import structlog
|
|
|
|
from app.config import settings
|
|
|
|
|
|
def configure_logging() -> None:
|
|
"""Configure structured logging."""
|
|
|
|
structlog.configure(
|
|
processors=[
|
|
structlog.stdlib.filter_by_level,
|
|
structlog.stdlib.add_logger_name,
|
|
structlog.stdlib.add_log_level,
|
|
structlog.stdlib.PositionalArgumentsFormatter(),
|
|
structlog.processors.TimeStamper(fmt="iso"),
|
|
structlog.processors.StackInfoRenderer(),
|
|
structlog.processors.format_exc_info,
|
|
structlog.processors.UnicodeDecoder(),
|
|
structlog.processors.JSONRenderer(
|
|
) if not settings.debug else structlog.dev.ConsoleRenderer(),
|
|
],
|
|
context_class=dict,
|
|
logger_factory=structlog.stdlib.LoggerFactory(),
|
|
wrapper_class=structlog.stdlib.BoundLogger,
|
|
cache_logger_on_first_use=True,
|
|
)
|
|
|
|
# Configure standard logging
|
|
logging.basicConfig(
|
|
format="%(message)s",
|
|
stream=sys.stdout,
|
|
level=getattr(logging, settings.log_level.upper()),
|
|
)
|
|
|
|
|
|
def get_logger(name: str) -> structlog.BoundLogger:
|
|
"""Get a configured logger instance."""
|
|
return structlog.get_logger(name) # type:ignore[no-any-return]
|