CI / lint (push) Canceled after 0s
CI / typecheck (push) Canceled after 0s
CI / unit-tests (push) Canceled after 0s
CI / integration-tests (push) Canceled after 0s
CI / container-build (push) Canceled after 0s
CI / dependency-audit (push) Canceled after 0s
Production-grade media metadata backend per docs/aurora-core-spec.md: Aurora-owned UUIDv7 identity system, normalized movie/TV/people/credits schema, provider abstraction with a TMDb adapter, an idempotent async ingestion pipeline (normalize -> match -> upsert -> provenance), a Postgres FTS/trigram search layer, a stateless FastAPI v1 API with Redis caching/rate-limiting, arq workers, dev/prod Docker Compose topologies, unit + integration + load tests, CI, and architecture/ database/API/ERD documentation. Co-Authored-By: Claude Sonnet 5 <[email protected]>
63 lines
1.9 KiB
Python
63 lines
1.9 KiB
Python
"""Structured, JSON-compatible logging with per-request correlation IDs.
|
|
|
|
Every request gets a request ID (from the `X-Request-ID` header if present,
|
|
otherwise generated) bound into the log context for the lifetime of that
|
|
request, via `RequestIDMiddleware` in `app.main`.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import sys
|
|
from contextvars import ContextVar
|
|
|
|
import structlog
|
|
|
|
from app.core.config import get_settings
|
|
|
|
request_id_ctx: ContextVar[str | None] = ContextVar("request_id", default=None)
|
|
|
|
|
|
def _add_request_id(_logger: object, _method_name: str, event_dict: dict) -> dict:
|
|
request_id = request_id_ctx.get()
|
|
if request_id is not None:
|
|
event_dict["request_id"] = request_id
|
|
return event_dict
|
|
|
|
|
|
def configure_logging() -> None:
|
|
settings = get_settings()
|
|
level = getattr(logging, settings.AURORA_LOG_LEVEL.upper(), logging.INFO)
|
|
|
|
logging.basicConfig(
|
|
format="%(message)s",
|
|
stream=sys.stdout,
|
|
level=level,
|
|
)
|
|
|
|
shared_processors: list = [
|
|
structlog.contextvars.merge_contextvars,
|
|
_add_request_id,
|
|
structlog.processors.add_log_level,
|
|
structlog.processors.TimeStamper(fmt="iso"),
|
|
structlog.processors.StackInfoRenderer(),
|
|
]
|
|
|
|
renderer: structlog.types.Processor
|
|
if settings.is_production or settings.AURORA_ENV != "development":
|
|
renderer = structlog.processors.JSONRenderer()
|
|
else:
|
|
renderer = structlog.dev.ConsoleRenderer()
|
|
|
|
structlog.configure(
|
|
processors=[*shared_processors, structlog.processors.format_exc_info, renderer],
|
|
wrapper_class=structlog.make_filtering_bound_logger(level),
|
|
context_class=dict,
|
|
logger_factory=structlog.PrintLoggerFactory(),
|
|
cache_logger_on_first_use=True,
|
|
)
|
|
|
|
|
|
def get_logger(name: str | None = None) -> structlog.stdlib.BoundLogger:
|
|
return structlog.get_logger(name)
|