Skip to content

Commit

Permalink
Integrate SQLAlchemy to log config (#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
florimondmanca authored Jul 1, 2022
1 parent fb27c4e commit 5cada35
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion server/infrastructure/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
5 changes: 5 additions & 0 deletions server/infrastructure/logging/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions tests/infrastructure/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
11 changes: 1 addition & 10 deletions tests/test_debugging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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

0 comments on commit 5cada35

Please sign in to comment.