Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion apps/api/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from contextlib import asynccontextmanager

from asgi_correlation_id import CorrelationIdMiddleware
from fastapi import FastAPI
from fastapi_problem.handler import add_exception_handler, new_exception_handler
Expand All @@ -9,16 +11,25 @@
from api.config import settings
from api.db.sqlalchemy_engine import get_sqlalchemy_engine
from api.health import health_router
from api.logging import StructLogMiddleware, setup_logging
from api.logging import StructLogMiddleware, setup_logging, suppress_uvicorn_access_logs
from api.otel.setup import setup_otel
from api.safety_queue import setup_safety_queue
from api.v5 import v5_router


@asynccontextmanager
async def lifespan(_app: FastAPI): # noqa: RUF029
# uvicorn adds its logger after fastapi app creation
# resupress uvicorn logging in init (after uvicorn has added)
suppress_uvicorn_access_logs()
yield


def create_app() -> FastAPI:
app = FastAPI(
title="Playground API",
version="0.1.0",
lifespan=lifespan,
)

add_exception_handler(app, new_exception_handler())
Expand All @@ -27,6 +38,7 @@ def create_app() -> FastAPI:
app.include_router(v5_router)

setup_logging(json_logs=settings.LOG_JSON_FORMAT, log_level=settings.LOG_LEVEL)

app.add_middleware(StructLogMiddleware)

app.add_middleware(CorrelationIdMiddleware)
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/api/logging/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Logging"""

from .middleware import StructLogMiddleware
from .setup import setup_logging
from .setup import setup_logging, suppress_uvicorn_access_logs

__all__ = ("StructLogMiddleware", "setup_logging")
__all__ = ("StructLogMiddleware", "setup_logging", "suppress_uvicorn_access_logs")
12 changes: 9 additions & 3 deletions apps/api/src/api/logging/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ def drop_color_message_key(_, __, event_dict: EventDict) -> EventDict:
return event_dict


def suppress_uvicorn_access_logs() -> None:
logging.getLogger("uvicorn.access").handlers.clear()
logging.getLogger("uvicorn.access").propagate = False


def setup_logging(*, json_logs: bool = False, log_level: str = "INFO"):
shared_processors: list[Processor] = [
structlog.contextvars.merge_contextvars,
Expand Down Expand Up @@ -58,6 +63,7 @@ def setup_logging(*, json_logs: bool = False, log_level: str = "INFO"):
handler = logging.StreamHandler()
handler.setFormatter(formatter)
root_logger = logging.getLogger()
root_logger.handlers.clear()
root_logger.addHandler(handler)
root_logger.setLevel(log_level.upper())

Expand All @@ -66,6 +72,6 @@ def setup_logging(*, json_logs: bool = False, log_level: str = "INFO"):
logging.getLogger(_log).handlers.clear()
logging.getLogger(_log).propagate = True

# Uvicorn logs are re-emitted with more context. We effectively silence them here
logging.getLogger("uvicorn.access").handlers.clear()
logging.getLogger("uvicorn.access").propagate = False
# Uvicorn logs are re-emitted with more context.
# supress uvicorn access logging -- also called from fastapi lifecycle
suppress_uvicorn_access_logs()
Loading