Add logging configuration and middleware for improved request tracking
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user