我正在使用烧瓶和werkzeug.为了监视从sqlalchemy发出的sql语句,我设置了一个logging.basicConfig()记录器,并附加了before_cursor_execute事件来监视SQL语句.但是现在werkzeug还将日志记录附加到该记录器,这不是我想要的.
所以我的日志看起来像这样……(不想要werkzeug消息)
INFO:root:SELECT anon_1.heartbeat_id AS anon_1_heartbeat_id
FROM (SELECT heartbeat.id AS heartbeat_id FROM heartbeat ORDER BY stamp desc LIMIT ?
OFFSET ?) AS anon_1 ORDER BY heartbeat_name
INFO:werkzeug:127.0.0.1 - - [13/Jun/2013 12:10:52] "GET / HTTP/1.1" 200 -
在werkzeug文档中,我找不到任何有关日志记录的信息.这是我正在使用的代码.
logging.basicConfig(filename='sql.log', level=logging.INFO)
def before_cursor_execute(conn, cursor, statement, parameters, context, executemany):
logging.info(statement)
event.listen(engine, "before_cursor_execute", before_cursor_execute)
解决方法:
尝试这样的事情(未经测试):
import logging
logger = logging.getLogger('sql')
logger.addHandler(logging.StreamHandler('sql.log'))
logger.setLevel(logging.INFO)
def before_cursor_execute(conn, cursor, statement, parameters, context, executemany):
logger.info(statement)
event.listen(engine, "before_cursor_execute", before_cursor_execute)
你做的只是创建一个不同的记录器,这样他们就不会混淆,这应该解决你遇到的问题:)