How do I configure sqlalchemy
to log the SQL statements that it's making to the database server, and also log the rows returned from those statements? This would be useful for debugging.
Option 1: set the sqlalchemy.engine
logger log level to either logging.INFO
or logging.DEBUG
:
e.g.,
>>> 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')
Reference: Configuring Logging
Option 2: use the echo
arg when calling sqlalchemy.create_engine()
:
e.g.,
>>> 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With