Add logging configuration and middleware for improved request tracking

This commit is contained in:
JSC
2025-07-25 10:54:30 +02:00
parent 2860008a6d
commit b6d1ef2a27
7 changed files with 137 additions and 4 deletions

View File

@@ -5,6 +5,7 @@ from sqlmodel import SQLModel
from sqlmodel.ext.asyncio.session import AsyncSession
from app.core.config import settings
from app.core.logging import get_logger
from app.models import ( # noqa: F401
plan,
playlist,
@@ -23,10 +24,12 @@ engine: AsyncEngine = create_async_engine(
async def get_db() -> AsyncGenerator[AsyncSession, None]:
logger = get_logger(__name__)
async with AsyncSession(engine) as session:
try:
yield session
except Exception:
except Exception as e:
logger.exception("Database session error: %s", e)
await session.rollback()
raise
finally:
@@ -34,5 +37,12 @@ async def get_db() -> AsyncGenerator[AsyncSession, None]:
async def init_db() -> None:
async with engine.begin() as conn:
await conn.run_sync(SQLModel.metadata.create_all)
logger = get_logger(__name__)
try:
logger.info("Initializing database tables")
async with engine.begin() as conn:
await conn.run_sync(SQLModel.metadata.create_all)
logger.info("Database tables created successfully")
except Exception as e:
logger.exception("Failed to initialize database: %s", e)
raise