Logging raw SQL statements

2,343 views
Skip to first unread message

George V. Reilly

unread,
Feb 9, 2011, 2:58:43 PM2/9/11
to sqlalchemy
Under SQLAlchemy 0.5, there used to be some logging setting that would
show the actual SQL queries that were being made. Alas, I forget the
exact invocation. Under SA 0.6, I have not been able to find a way to
do this, short of hacking the SA source. Here's the closest that I've
been able to come

logging.basicConfig(level=logging.DEBUG)
logging.getLogger('sqlalchemy').setLevel(logging.DEBUG)
logging.getLogger('sqlalchemy.engine').setLevel(logging.DEBUG)
logging.getLogger('sqlalchemy.pool').setLevel(logging.DEBUG)

logging.getLogger('sqlalchemy.dialects.mysql').setLevel(logging.DEBUG)
logging.getLogger('sqlalchemy.orm').setLevel(logging.DEBUG)
logging.getLogger('mysqldb').setLevel(logging.DEBUG)

but I see no SELECTs in the output. Obviously, this is useful when we
want the server to explain query plans and the like, to understand
performance issues.

I specifically care about MySQL and SQLite queries.

/George Reilly, Seattle

Michael Bayer

unread,
Feb 9, 2011, 3:54:31 PM2/9/11
to sqlal...@googlegroups.com

The above statements are correct. 'sqlalchemy.engine' specifically targets the SQL statements emitted.

There is also "echo=True" on create_engine() which is a shortcut for the "sqlalchemy.engine" statement above.

A description of logging as applied to SQLAlchemy is at http://www.sqlalchemy.org/docs/core/engines.html#configuring-logging .

Note that when using logging, the context of the application may suppress standard output. The test harness "nose" does so unless the -s flag is used, and a daemonizing server like paster server also shuts off standard output. You may need to route the logging to a file instead in a case like the latter.

George V. Reilly

unread,
Feb 9, 2011, 5:33:02 PM2/9/11
to sqlal...@googlegroups.com
The combination of create_engine(..., echo=True) and
  logging.basicConfig(level=logging.DEBUG)
  logging.getLogger('sqlalchemy').setLevel(logging.DEBUG)
does the trick for me now.

Thanks!
/George

Michael Bayer

unread,
Feb 9, 2011, 6:02:52 PM2/9/11
to sqlal...@googlegroups.com
in theory that would log all statements twice, only one should be needed.....

> --
> You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
> To post to this group, send email to sqlal...@googlegroups.com.
> To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.

Reply all
Reply to author
Forward
0 new messages