Skip to content

Commit 47d0e25

Browse files
author
James Robinson
authored
Merge pull request #3 from noteable-io/better_more_logging
Better more logging
2 parents 2d19d75 + 1bfaa0a commit 47d0e25

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

asyncpg/connection.py

+23-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import asyncpg
1010
import collections
1111
import collections.abc
12+
from datetime import datetime, timezone
1213
import functools
1314
import itertools
1415
import inspect
@@ -305,6 +306,25 @@ def is_in_transaction(self):
305306
"""
306307
return self._protocol.is_in_transaction()
307308

309+
def log_statement(self, context: str, statement: str, args=None):
310+
when = datetime.now(timezone.utc)
311+
312+
if self._recent_statements:
313+
most_recently_logged = self._recent_statements[-1]
314+
# Each entry is (timestamp, calling context, sql statement, args (if any)) tuple.
315+
(
316+
_,
317+
_,
318+
prior_statement,
319+
_,
320+
) = most_recently_logged
321+
if prior_statement == statement:
322+
# Do not double-log same query.
323+
return
324+
325+
to_log = (when.strftime("%Y-%m-%d %H:%M:%S UTC"), context, statement, args)
326+
self._recent_statements.append(to_log)
327+
308328
async def execute(self, query: str, *args, timeout: float = None) -> str:
309329
"""Execute an SQL command (or commands).
310330
@@ -335,12 +355,8 @@ async def execute(self, query: str, *args, timeout: float = None) -> str:
335355
"""
336356
self._check_open()
337357

338-
# Append to circular buffer of most recent executed statements
339-
# for debugging.
340-
self._recent_statements.append(query)
341-
342358
if not args:
343-
self._recent_statements.append(query)
359+
self.log_statement("execute no args", query)
344360
return await self._protocol.query(query, timeout)
345361

346362
_, status, _ = await self._execute(
@@ -541,7 +557,7 @@ def cursor(self, query, *args, prefetch=None, timeout=None, record_class=None):
541557
"""
542558
self._check_open()
543559

544-
self._recent_statements.append(query)
560+
self.log_statement("cursor", query, args)
545561

546562
return cursor.CursorFactory(
547563
self,
@@ -1802,7 +1818,7 @@ async def _do_execute(
18021818
ignore_custom_codec=False,
18031819
record_class=None,
18041820
):
1805-
self._recent_statements.append(query)
1821+
self.log_statement("_do_execute", query)
18061822
if timeout is None:
18071823
stmt = await self._get_statement(
18081824
query,

asyncpg/prepared_stmt.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
class PreparedStatement(connresource.ConnectionResource):
1616
"""A representation of a prepared statement."""
1717

18-
__slots__ = ("_state", "_query", "_last_status")
18+
__slots__ = ("_state", "_query", "_last_status", "_logged_args")
1919

2020
def __init__(self, connection, query, state):
2121
super().__init__(connection)
@@ -219,13 +219,16 @@ async def executemany(self, args, *, timeout: float = None):
219219
220220
.. versionadded:: 0.22.0
221221
"""
222+
self._logged_args = args
222223
return await self.__do_execute(
223224
lambda protocol: protocol.bind_execute_many(self._state, args, "", timeout)
224225
)
225226

226227
async def __do_execute(self, executor):
227228
protocol = self._connection._protocol
228-
self._connection._recent_statements.append(self._query)
229+
self._connection.log_statement(
230+
"preparedstatement __do_execute", self._query, self._logged_args
231+
)
229232
try:
230233
return await executor(protocol)
231234
except exceptions.OutdatedSchemaCacheError:
@@ -238,6 +241,7 @@ async def __do_execute(self, executor):
238241
raise
239242

240243
async def __bind_execute(self, args, limit, timeout):
244+
self._logged_args = args
241245
data, status, _ = await self.__do_execute(
242246
lambda protocol: protocol.bind_execute(
243247
self._state, args, "", limit, True, timeout

0 commit comments

Comments
 (0)