Add structured logging throughout the backend

Request-level logging middleware (method/path/status/duration),
per-module loggers in routes/services (nearby search radius
expansion, 404s, name search), LOG_LEVEL setting wired through
docker-compose, and seed_loader converted from print() to logging.

Verified: pytest passes, and manually confirmed log lines appear for
both successful and 404 requests via `docker compose logs api`.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
vrubelroman 2026-07-09 20:10:10 +00:00
parent 188bf01d6e
commit be67902b45
11 changed files with 114 additions and 4 deletions

View file

@ -1,9 +1,26 @@
import logging
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
from fastapi import FastAPI
from app.api.routes import cities, health, nearby, places
from app.config import settings
from app.core.logging import configure_logging, log_requests
app = FastAPI(title="guideCity API", version="0.1.0")
configure_logging(level=getattr(logging, settings.log_level.upper(), logging.INFO))
logger = logging.getLogger("guidecity")
@asynccontextmanager
async def lifespan(_app: FastAPI) -> AsyncIterator[None]:
logger.info("guideCity API startup complete (log_level=%s)", settings.log_level)
yield
logger.info("guideCity API shutting down")
app = FastAPI(title="guideCity API", version="0.1.0", lifespan=lifespan)
app.middleware("http")(log_requests)
app.include_router(health.router, prefix=settings.api_v1_prefix)
app.include_router(cities.router, prefix=settings.api_v1_prefix)