我想记录 psycopg2 进行的查询,但是 psycopg2 documentation并没有真正指定应该如何使用 LoggingConnection。
import logging
from psycopg2.extras import LoggingConnection
db_settings = {
"user": "abcd",
"password": "efgh",
"host": "postgres.db",
"database": "dev",
}
conn = LoggingConnection(**db_settings)
报错
LoggingConnection(**db_settings) TypeError: function takes at most 2 arguments (5 given)
最佳答案
似乎设置 connection_factory=LoggingConnection
有效
import logging
import psycopg2
from psycopg2.extras import LoggingConnection
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
db_settings = {
"user": "abcd",
"password": "efgh",
"host": "postgres.db",
"database": "dev",
}
conn = psycopg2.connect(connection_factory=LoggingConnection, **db_settings)
conn.initialize(logger)
cur = conn.cursor()
cur.execute("SELECT * FROM table LIMIT 5")
关于python - 我如何使用 Psycopg2 的 LoggingConnection?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28950212/