Skip to content

Commit cbf370b

Browse files
committed
align logs of aiopg and asyncpg
1 parent a1ce56d commit cbf370b

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

packages/service-library/src/servicelib/aiohttp/db_asyncpg_engine.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from sqlalchemy.ext.asyncio import AsyncEngine
1818

1919
from ..db_asyncpg_utils import create_async_engine_and_pg_database_ready
20+
from ..logging_utils import log_context
2021

2122
APP_DB_ASYNC_ENGINE_KEY: Final[str] = f"{__name__ }.AsyncEngine"
2223

@@ -49,8 +50,14 @@ async def connect_to_db(app: web.Application, settings: PostgresSettings) -> Non
4950
update={"POSTGRES_CLIENT_NAME": settings.POSTGRES_CLIENT_NAME + "-asyncpg"}
5051
)
5152

52-
engine = await create_async_engine_and_pg_database_ready(settings)
53-
_set_async_engine_to_app_state(app, engine)
53+
with log_context(
54+
_logger,
55+
logging.INFO,
56+
"Connecting app[APP_DB_ASYNC_ENGINE_KEY] to postgres with %s",
57+
f"{settings=}",
58+
):
59+
engine = await create_async_engine_and_pg_database_ready(settings)
60+
_set_async_engine_to_app_state(app, engine)
5461

5562
_logger.info(
5663
"app[APP_DB_ASYNC_ENGINE_KEY] ready : %s",
@@ -60,6 +67,8 @@ async def connect_to_db(app: web.Application, settings: PostgresSettings) -> Non
6067

6168
async def close_db_connection(app: web.Application) -> None:
6269
engine = get_async_engine(app)
63-
with log_context(_logger, logging.DEBUG, f"db disconnect of {engine}"):
70+
with log_context(
71+
_logger, logging.DEBUG, f"app[APP_DB_ASYNC_ENGINE_KEY] disconnect of {engine}"
72+
):
6473
if engine:
6574
await engine.dispose()

services/web/server/src/simcore_service_webserver/db/_aiopg.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from models_library.utils.json_serialization import json_dumps
1414
from servicelib.aiohttp.aiopg_utils import is_pg_responsive
1515
from servicelib.aiohttp.application_keys import APP_AIOPG_ENGINE_KEY
16+
from servicelib.logging_utils import log_context
1617
from servicelib.retry_policies import PostgresRetryPolicyUponInitialization
1718
from simcore_postgres_database.errors import DBAPIError
1819
from simcore_postgres_database.utils_aiopg import (
@@ -30,8 +31,6 @@
3031

3132
@retry(**PostgresRetryPolicyUponInitialization(_logger).kwargs)
3233
async def _ensure_pg_ready(settings: PostgresSettings) -> Engine:
33-
34-
_logger.info("Connecting to postgres with %s", f"{settings=}")
3534
engine: Engine = await create_engine(
3635
settings.dsn,
3736
application_name=settings.POSTGRES_CLIENT_NAME,
@@ -45,15 +44,21 @@ async def _ensure_pg_ready(settings: PostgresSettings) -> Engine:
4544
await close_engine(engine)
4645
raise
4746

48-
_logger.info("Connection to postgres with %s succeeded", f"{settings=}")
4947
return engine # tenacity rules guarantee exit with exc
5048

5149

5250
async def postgres_cleanup_ctx(app: web.Application) -> AsyncIterator[None]:
5351

5452
settings = get_plugin_settings(app)
55-
aiopg_engine = await _ensure_pg_ready(settings)
56-
app[APP_AIOPG_ENGINE_KEY] = aiopg_engine
53+
54+
with log_context(
55+
_logger,
56+
logging.INFO,
57+
"Connecting app[APP_AIOPG_ENGINE_KEY] to postgres with %s",
58+
f"{settings=}",
59+
):
60+
aiopg_engine = await _ensure_pg_ready(settings)
61+
app[APP_AIOPG_ENGINE_KEY] = aiopg_engine
5762

5863
_logger.info(
5964
"app[APP_AIOPG_ENGINE_KEY] created %s",

0 commit comments

Comments
 (0)