add logger

This commit is contained in:
Dohyun Lim committed 2026-01-14 17:46:45 +09:00
1 parent 1acd8846ab
commit bf7b53c8e8
41 files changed
+1832 -694

No files matched your search

+15 -12
View File
@@ -4,8 +4,11 @@ from typing import AsyncGenerator
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from sqlalchemy.orm import DeclarativeBase
from app.utils.logger import get_logger
from config import db_settings
logger = get_logger("database")
class Base(DeclarativeBase):
pass
@@ -74,7 +77,7 @@ async def create_db_tables():
from app.song.models import Song # noqa: F401
from app.video.models import Video # noqa: F401
print("Creating database tables...")
logger.info("Creating database tables...")
async with asyncio.timeout(10):
async with engine.begin() as connection:
@@ -87,7 +90,7 @@ async def get_session() -> AsyncGenerator[AsyncSession, None]:
pool = engine.pool
# 커넥션 풀 상태 로깅 (디버깅용)
print(
logger.debug(
f"[get_session] ACQUIRE - pool_size: {pool.size()}, "
f"in: {pool.checkedin()}, out: {pool.checkedout()}, "
f"overflow: {pool.overflow()}"
@@ -95,7 +98,7 @@ async def get_session() -> AsyncGenerator[AsyncSession, None]:
async with AsyncSessionLocal() as session:
acquire_time = time.perf_counter()
print(
logger.debug(
f"[get_session] Session acquired in "
f"{(acquire_time - start_time)*1000:.1f}ms"
)
@@ -103,14 +106,14 @@ async def get_session() -> AsyncGenerator[AsyncSession, None]:
yield session
except Exception as e:
await session.rollback()
print(
logger.error(
f"[get_session] ROLLBACK - error: {type(e).__name__}: {e}, "
f"duration: {(time.perf_counter() - start_time)*1000:.1f}ms"
)
raise e
finally:
total_time = time.perf_counter() - start_time
print(
logger.debug(
f"[get_session] RELEASE - duration: {total_time*1000:.1f}ms, "
f"pool_out: {pool.checkedout()}"
)
@@ -121,7 +124,7 @@ async def get_background_session() -> AsyncGenerator[AsyncSession, None]:
start_time = time.perf_counter()
pool = background_engine.pool
print(
logger.debug(
f"[get_background_session] ACQUIRE - pool_size: {pool.size()}, "
f"in: {pool.checkedin()}, out: {pool.checkedout()}, "
f"overflow: {pool.overflow()}"
@@ -129,7 +132,7 @@ async def get_background_session() -> AsyncGenerator[AsyncSession, None]:
async with BackgroundSessionLocal() as session:
acquire_time = time.perf_counter()
print(
logger.debug(
f"[get_background_session] Session acquired in "
f"{(acquire_time - start_time)*1000:.1f}ms"
)
@@ -137,7 +140,7 @@ async def get_background_session() -> AsyncGenerator[AsyncSession, None]:
yield session
except Exception as e:
await session.rollback()
print(
logger.error(
f"[get_background_session] ROLLBACK - "
f"error: {type(e).__name__}: {e}, "
f"duration: {(time.perf_counter() - start_time)*1000:.1f}ms"
@@ -145,7 +148,7 @@ async def get_background_session() -> AsyncGenerator[AsyncSession, None]:
raise e
finally:
total_time = time.perf_counter() - start_time
print(
logger.debug(
f"[get_background_session] RELEASE - "
f"duration: {total_time*1000:.1f}ms, "
f"pool_out: {pool.checkedout()}"
@@ -154,8 +157,8 @@ async def get_background_session() -> AsyncGenerator[AsyncSession, None]:
# 앱 종료 시 엔진 리소스 정리 함수
async def dispose_engine() -> None:
print("[dispose_engine] Disposing database engines...")
logger.info("[dispose_engine] Disposing database engines...")
await engine.dispose()
print("[dispose_engine] Main engine disposed")
logger.info("[dispose_engine] Main engine disposed")
await background_engine.dispose()
print("[dispose_engine] Background engine disposed - ALL DONE")
logger.info("[dispose_engine] Background engine disposed - ALL DONE")