asyncpg icon indicating copy to clipboard operation
asyncpg copied to clipboard

feature request: query logging

Open dmig-alarstudios opened this issue 5 years ago • 4 comments

I could not find any issue/question/pr related to this, so I'm starting a new one.

It would be great to have query logging implemented inside asyncpg:

  • to see all executed queries and their parameters
  • especially nice would be to have a query cache logging to see cache hits/misses

dmig-alarstudios avatar Sep 30 '20 10:09 dmig-alarstudios

Not a bad idea. Patches are welcome.

elprans avatar Sep 30 '20 17:09 elprans

Are there any performance tests in the repo?

dmig avatar Oct 01 '20 08:10 dmig

Could you please have a look at changes while I'm fixing and writing new tests?

dmig-alarstudios avatar Oct 01 '20 14:10 dmig-alarstudios

@elprans any updates guys?

maksimartemevalar avatar May 11 '22 09:05 maksimartemevalar

It's possible to implement this oneself!

import logging
from asyncpg.connection import Connection


class LoggingConnection(Connection):

    _query_logger = logging.getLogger(__name__)

    async def _execute(self, query, args, *posargs, **kwargs):
        self._query_logger.info(
            f"Executing query:\n" +
            '\n'.join('  ' + line for line in query.splitlines()) +
            f"...with args: {args}")
        return await Connection._execute(self, query, args, *posargs, **kwargs)

Now just pass ConnectionLogger as the connection_class parameter of asyncpg.create_pool.

bayersglassey-zesty avatar Mar 07 '23 05:03 bayersglassey-zesty

Implemented in #1043.

elprans avatar Oct 09 '23 20:10 elprans