42 lines
1.1 KiB
Python
42 lines
1.1 KiB
Python
"""
|
|
app/logging_setup.py
|
|
Centralised logging configuration for the application.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import sys
|
|
from typing import TYPE_CHECKING
|
|
|
|
if TYPE_CHECKING:
|
|
from app.config import Settings
|
|
|
|
|
|
def configure_logging(settings: "Settings") -> None:
|
|
"""Configure root logger and suppress noisy third-party loggers."""
|
|
level = getattr(logging, settings.log_level.upper(), logging.INFO)
|
|
|
|
formatter = logging.Formatter(
|
|
fmt="%(asctime)s %(levelname)-8s %(name)s %(message)s",
|
|
datefmt="%Y-%m-%d %H:%M:%S",
|
|
)
|
|
|
|
handler = logging.StreamHandler(sys.stdout)
|
|
handler.setFormatter(formatter)
|
|
|
|
root = logging.getLogger()
|
|
root.setLevel(level)
|
|
# Avoid duplicate handlers if called multiple times
|
|
if not root.handlers:
|
|
root.addHandler(handler)
|
|
else:
|
|
root.handlers[0] = handler
|
|
|
|
# Quieten noisy third-party loggers
|
|
for noisy in ("uvicorn.access", "uvicorn.error", "httpx", "httpcore"):
|
|
logging.getLogger(noisy).setLevel(logging.WARNING)
|
|
|
|
logging.getLogger("uvicorn").setLevel(logging.INFO)
|
|
|