Last active
June 4, 2022 08:43
-
-
Save xflr6/84f9bc44b82ae1f0347b to your computer and use it in GitHub Desktop.
Use psycopg2 LoggingConnection with sqlalchemy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Use psycopg2 LoggingConnection with SQLAlalchemy.""" | |
import logging | |
import psycopg2.extras | |
import sqlalchemy as sa | |
logging.basicConfig(level=logging.DEBUG) | |
log = logging.getLogger() | |
class LoggingConnection(psycopg2.extras.LoggingConnection): | |
def __init__(self, *args, **kwargs) -> None: | |
super().__init__(*args, **kwargs) | |
self.initialize(log) | |
engine = sa.create_engine('postgresql://postgres@/', | |
connect_args={'connection_factory': LoggingConnection}) | |
print(engine.scalar(sa.func.version())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment