Pau*_*aul 3 python logging werkzeug flask
我正在使用烧瓶和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 -
Run Code Online (Sandbox Code Playgroud)
在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)
Run Code Online (Sandbox Code Playgroud)
尝试这样的事情(未经测试):
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)
Run Code Online (Sandbox Code Playgroud)
您要做的只是创建一个不同的记录器,这样它们就不会混淆,这应该可以解决您遇到的问题:)
小智 5
Sqlalchemy使用"sqlalchemy.engine"记录器.要进行配置,您可以执行以下操作:
lg = logging.getLogger('sqlalchemy.engine')
lg.setLevel(logging.INFO)
lh = logging.StreamHandler()
lh.setFormatter(logging.Formatter('YOUR FORMAT HERE'))
lg.add_handler(lh)
Run Code Online (Sandbox Code Playgroud)
之后,你可以使用
logging.getLogger('sqlalchemy.engine')
logging.info('your sql statement')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2641 次 |
| 最近记录: |