20

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.

1 Answer 1

27

Option 1: set the sqlalchemy.engine logger log level to either logging.INFO or logging.DEBUG

import logging
logging.basicConfig()
logger = logging.getLogger('sqlalchemy.engine')
logger.setLevel(logging.DEBUG)

# example query
session.query(User).all()

Example output:

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()

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()

# example query
users = session.query(User).all()

Example output:

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.

Sign up to request clarification or add additional context in comments.

2 Comments

seems like people have trouble finding this in the docs but you can also use the Python logging API directly to fully customize how logging runs.
Thanks @zzzeek! I have now added that to the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.