MCPcopy Create free account
hub / github.com/MagicStack/asyncpg / query_logger

Method query_logger

asyncpg/connection.py:1916–1945  ·  view source on GitHub ↗

Context manager that adds `callback` to the list of query loggers, and removes it upon exit. :param callable callback: A callable or a coroutine function receiving one argument: **record**, a LoggedQuery containing `query`, `args`, `timeout`, `ela

(self, callback)

Source from the content-addressed store, hash-verified

1914
1915 @contextlib.contextmanager
1916 def query_logger(self, callback):
1917 """Context manager that adds `callback` to the list of query loggers,
1918 and removes it upon exit.
1919
1920 :param callable callback:
1921 A callable or a coroutine function receiving one argument:
1922 **record**, a LoggedQuery containing `query`, `args`, `timeout`,
1923 `elapsed`, `exception`, `conn_addr`, and `conn_params`.
1924
1925 Example:
1926
1927 .. code-block:: pycon
1928
1929 >>> class QuerySaver:
1930 def __init__(self):
1931 self.queries = []
1932 def __call__(self, record):
1933 self.queries.append(record.query)
1934 >>> with con.query_logger(QuerySaver()):
1935 >>> await con.execute("SELECT 1")
1936 >>> print(log.queries)
1937 ['SELECT 1']
1938
1939 .. versionadded:: 0.29.0
1940 """
1941 self.add_query_logger(callback)
1942 try:
1943 yield
1944 finally:
1945 self.remove_query_logger(callback)
1946
1947 @contextlib.contextmanager
1948 def _time_and_log(self, query, args, timeout):

Callers 3

test_logging_contextMethod · 0.80
test_error_loggingMethod · 0.80

Calls 2

add_query_loggerMethod · 0.95
remove_query_loggerMethod · 0.95

Tested by 3

test_logging_contextMethod · 0.64
test_error_loggingMethod · 0.64