Skip to content

Commit

Permalink
refactor(api): refactor logging to newer fastapi command
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpomerenke committed Apr 10, 2024
1 parent 1ed3af7 commit b6af8d4
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions backend-python/media_impact_monitor/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

import logging
from contextlib import asynccontextmanager

from fastapi import FastAPI, HTTPException
from joblib import hash as joblib_hash
Expand Down Expand Up @@ -40,6 +41,17 @@
docs_url=None,
)


@asynccontextmanager
async def app_lifespan(app: FastAPI):
logger = logging.getLogger("uvicorn.access")
console_formatter = AccessFormatter(
"{asctime} {levelprefix} {message}", style="{", use_colors=True
)
logger.handlers[0].setFormatter(console_formatter)
yield


app = FastAPI(**metadata)

app.add_middleware(
Expand All @@ -51,11 +63,13 @@
)


logger = logging.getLogger("uvicorn.access")
console_formatter = AccessFormatter(
"{asctime} {levelprefix} {message}", style="{", use_colors=True
)
logger.handlers[0].setFormatter(console_formatter)
@app.on_event("startup")
async def startup_event():
logger = logging.getLogger("uvicorn.access")
console_formatter = AccessFormatter(
"{asctime} {levelprefix} {message}", style="{", use_colors=True
)
logger.handlers[0].setFormatter(console_formatter)


@app.get("/", include_in_schema=False)
Expand Down

0 comments on commit b6af8d4

Please sign in to comment.