diff --git a/server/infrastructure/database.py b/server/infrastructure/database.py index a1a27953..293e88a2 100644 --- a/server/infrastructure/database.py +++ b/server/infrastructure/database.py @@ -28,7 +28,7 @@ class Base(metaclass=DeclarativeMeta): class Database: def __init__(self, url: str, debug: bool = False) -> None: - self._engine = create_async_engine(url, echo=debug, future=True) + self._engine = create_async_engine(url, future=True) self._session_cls = sessionmaker( bind=self._engine, class_=AsyncSession, future=True ) diff --git a/server/infrastructure/logging/config.py b/server/infrastructure/logging/config.py index db65029c..38cffa54 100644 --- a/server/infrastructure/logging/config.py +++ b/server/infrastructure/logging/config.py @@ -28,6 +28,11 @@ def get_log_config(settings: Settings) -> dict: "handlers": ["default"], "level": "DEBUG" if settings.debug else "INFO", }, + "sqlalchemy.engine": { + "handlers": ["default"] if settings.debug else [], + "level": "INFO", + "propagate": False, + }, "uvicorn.error": { "handlers": ["default"], "level": "INFO", diff --git a/tests/infrastructure/test_logging.py b/tests/infrastructure/test_logging.py index cf6a63e1..75e4998a 100644 --- a/tests/infrastructure/test_logging.py +++ b/tests/infrastructure/test_logging.py @@ -22,6 +22,10 @@ def test_logging(capsys: pytest.CaptureFixture) -> None: assert "Debug test" not in captured.out assert "server.example: Info test" in captured.out + sqlalchemy_logger = logging.getLogger("sqlalchemy.engine") + assert sqlalchemy_logger.level == logging.INFO + assert not sqlalchemy_logger.handlers + def test_logging_debug( monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture @@ -42,6 +46,10 @@ def test_logging_debug( assert not captured.err assert "server.example: Debug test" in captured.out + sqlalchemy_logger = logging.getLogger("sqlalchemy.engine") + assert sqlalchemy_logger.level == logging.INFO + assert sqlalchemy_logger.handlers[0].name == "default" + def test_logging_live_renders_json( monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture diff --git a/tests/test_debugging.py b/tests/test_debugging.py index a5842637..8e54ce3c 100644 --- a/tests/test_debugging.py +++ b/tests/test_debugging.py @@ -2,9 +2,8 @@ import pytest from server.api.app import App, create_app -from server.config.di import configure, resolve +from server.config.di import configure from server.config.settings import Settings -from server.infrastructure.database import Database from server.seedwork.application.di import Container from .helpers import create_client @@ -14,9 +13,6 @@ async def test_debug_default_disabled(app: App, client: httpx.AsyncClient) -> None: assert not app.debug - db = resolve(Database) - assert not db.engine.echo - response = await client.get("/_debug_toolbar") assert response.status_code == 404 @@ -31,11 +27,6 @@ async def test_debug_enabled(monkeypatch: pytest.MonkeyPatch) -> None: settings = container.resolve(Settings) app = create_app(settings) - assert app.debug - - db = container.resolve(Database) - assert db.engine.echo - async with create_client(app) as client: response = await client.get("/_debug_toolbar") assert response.status_code != 404