MCPcopy
hub / github.com/MagicStack/asyncpg / query_logger

Method query_logger

asyncpg/connection.py:1885–1912  ·  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

1883
1884 @contextlib.contextmanager
1885 def query_logger(self, callback):
1886 """Context manager that adds `callback` to the list of query loggers,
1887 and removes it upon exit.
1888
1889 :param callable callback:
1890 A callable or a coroutine function receiving one argument:
1891 **record**, a LoggedQuery containing `query`, `args`, `timeout`,
1892 `elapsed`, `exception`, `conn_addr`, and `conn_params`.
1893
1894 Example:
1895
1896 .. code-block:: pycon
1897
1898 >>> class QuerySaver:
1899 def __init__(self):
1900 self.queries = []
1901 def __call__(self, record):
1902 self.queries.append(record.query)
1903 >>> with con.query_logger(QuerySaver()):
1904 >>> await con.execute("SELECT 1")
1905 >>> print(log.queries)
1906 ['SELECT 1']
1907
1908 .. versionadded:: 0.29.0
1909 """
1910 self.add_query_logger(callback)
1911 yield
1912 self.remove_query_logger(callback)
1913
1914 @contextlib.contextmanager
1915 def _time_and_log(self, query, args, timeout):

Callers 2

test_logging_contextMethod · 0.80
test_error_loggingMethod · 0.80

Calls 2

add_query_loggerMethod · 0.95
remove_query_loggerMethod · 0.95

Tested by 2

test_logging_contextMethod · 0.64
test_error_loggingMethod · 0.64