Add logging configuration and middleware for improved request tracking
This commit is contained in:
16
app/main.py
16
app/main.py
@@ -4,20 +4,36 @@ from contextlib import asynccontextmanager
|
||||
from fastapi import FastAPI
|
||||
|
||||
from app.core.database import init_db
|
||||
from app.core.logging import get_logger, setup_logging
|
||||
from app.middleware.logging import LoggingMiddleware
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(_app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
"""Application lifespan context manager for setup and teardown."""
|
||||
setup_logging()
|
||||
logger = get_logger(__name__)
|
||||
logger.info("Starting application")
|
||||
|
||||
await init_db()
|
||||
logger.info("Database initialized")
|
||||
|
||||
yield
|
||||
|
||||
logger.info("Shutting down application")
|
||||
|
||||
|
||||
def create_app() -> FastAPI:
|
||||
"""Create and configure the FastAPI application."""
|
||||
app = FastAPI(lifespan=lifespan)
|
||||
|
||||
app.add_middleware(LoggingMiddleware)
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
@app.get("/")
|
||||
def health() -> dict[str, str]:
|
||||
logger.info("Health check endpoint accessed")
|
||||
return {"status": "healthy"}
|
||||
|
||||
return app
|
||||
|
||||
Reference in New Issue
Block a user