如何记录 sqlalchemy 中返回的 SQL 语句和行以帮助调试?
How to log SQL statements and rows returned in sqlalchemy to aid in debugging?
如何配置 sqlalchemy
以记录它对数据库服务器所做的 SQL 语句,并记录从这些语句返回的行?这对调试很有用。
选项 1:将 sqlalchemy.engine
记录器日志级别设置为 logging.INFO
或 logging.DEBUG
:
例如,
>>> import logging
>>> logging.basicConfig()
>>> logger = logging.getLogger('sqlalchemy.engine')
>>> logger.setLevel(logging.DEBUG)
>>> session.query(User).all()
2015-01-02 11:54:25,854 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2015-01-02 11:54:25,856 INFO sqlalchemy.engine.base.Engine SELECT users.id AS users_id, users.name AS users_name
FROM users
2015-01-02 11:54:25,857 INFO sqlalchemy.engine.base.Engine {}
2015-01-02 11:54:25,858 DEBUG sqlalchemy.engine.base.Engine Col ('users_id', 'users_name')
2015-01-02 11:54:25,860 DEBUG sqlalchemy.engine.base.Engine Row (1, u'Alice')
2015-01-02 11:54:25,860 DEBUG sqlalchemy.engine.base.Engine Row (2, u'Bob')
选项 2:调用 sqlalchemy.create_engine()
时使用 echo
参数:
例如,
>>> from sqlalchemy import create_engine
>>> from sqlalchemy.orm import sessionmaker
>>> engine = create_engine('postgres://postgres_user:my_password@localhost/my_db',
... echo="debug")
>>> Session = sessionmaker(bind=engine)
>>> session = Session()
>>> users = session.query(User).all()
2015-01-02 11:54:25,854 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2015-01-02 11:54:25,856 INFO sqlalchemy.engine.base.Engine SELECT users.id AS users_id, users.name AS users_name
FROM users
2015-01-02 11:54:25,857 INFO sqlalchemy.engine.base.Engine {}
2015-01-02 11:54:25,858 DEBUG sqlalchemy.engine.base.Engine Col ('users_id', 'users_name')
2015-01-02 11:54:25,860 DEBUG sqlalchemy.engine.base.Engine Row (1, u'Alice')
2015-01-02 11:54:25,860 DEBUG sqlalchemy.engine.base.Engine Row (2, u'Bob')
Per the sqlalchemy documentation:
*sqlalchemy.create_engine (*args, **kwargs)
...
echo=False – if True, the Engine will log all statements as well as a repr() of their parameter lists to the engines logger, which defaults to sys.stdout. The echo attribute of Engine can be modified at any time to turn logging on and off. If set to the string "debug", result rows will be printed to the standard output as well.
如何配置 sqlalchemy
以记录它对数据库服务器所做的 SQL 语句,并记录从这些语句返回的行?这对调试很有用。
选项 1:将 sqlalchemy.engine
记录器日志级别设置为 logging.INFO
或 logging.DEBUG
:
例如,
>>> import logging
>>> logging.basicConfig()
>>> logger = logging.getLogger('sqlalchemy.engine')
>>> logger.setLevel(logging.DEBUG)
>>> session.query(User).all()
2015-01-02 11:54:25,854 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2015-01-02 11:54:25,856 INFO sqlalchemy.engine.base.Engine SELECT users.id AS users_id, users.name AS users_name
FROM users
2015-01-02 11:54:25,857 INFO sqlalchemy.engine.base.Engine {}
2015-01-02 11:54:25,858 DEBUG sqlalchemy.engine.base.Engine Col ('users_id', 'users_name')
2015-01-02 11:54:25,860 DEBUG sqlalchemy.engine.base.Engine Row (1, u'Alice')
2015-01-02 11:54:25,860 DEBUG sqlalchemy.engine.base.Engine Row (2, u'Bob')
选项 2:调用 sqlalchemy.create_engine()
时使用 echo
参数:
例如,
>>> from sqlalchemy import create_engine
>>> from sqlalchemy.orm import sessionmaker
>>> engine = create_engine('postgres://postgres_user:my_password@localhost/my_db',
... echo="debug")
>>> Session = sessionmaker(bind=engine)
>>> session = Session()
>>> users = session.query(User).all()
2015-01-02 11:54:25,854 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2015-01-02 11:54:25,856 INFO sqlalchemy.engine.base.Engine SELECT users.id AS users_id, users.name AS users_name
FROM users
2015-01-02 11:54:25,857 INFO sqlalchemy.engine.base.Engine {}
2015-01-02 11:54:25,858 DEBUG sqlalchemy.engine.base.Engine Col ('users_id', 'users_name')
2015-01-02 11:54:25,860 DEBUG sqlalchemy.engine.base.Engine Row (1, u'Alice')
2015-01-02 11:54:25,860 DEBUG sqlalchemy.engine.base.Engine Row (2, u'Bob')
Per the sqlalchemy documentation:
*sqlalchemy.create_engine (*args, **kwargs)
...
echo=False – if True, the Engine will log all statements as well as a repr() of their parameter lists to the engines logger, which defaults to sys.stdout. The echo attribute of Engine can be modified at any time to turn logging on and off. If set to the string "debug", result rows will be printed to the standard output as well.