Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to prevent werkzeug from logging

I'm using flask and werkzeug. To monitor sql statements emitted from sqlalchemy I've set up a logging.basicConfig() logger and attached the before_cursor_execute event to monitor SQL statements. But now werkzeug also attaches the logging to that logger, that's not what I want. so my log looks like this...(the werkzeug message is not wanted)

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 -

In the werkzeug documentation I can't find anything about logging. And here is the code I'm using.

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)
like image 849
Paul Avatar asked Jun 13 '13 10:06

Paul


People also ask

What is werkzeug logging?

Werkzeug is a comprehensive WSGI web application library. It began as a simple collection of various utilities for WSGI applications and has become one of the most advanced WSGI utility libraries.

Is flask secure for production?

Although Flask has a built-in web server, as we all know, it's not suitable for production and needs to be put behind a real web server able to communicate with Flask through a WSGI protocol. A common choice for that is Gunicorn—a Python WSGI HTTP server.

Does flask come with werkzeug?

Does Flask use Werkzeug? It started as a simple collection of WSGI application utilities and has evolved into one of the most powerful WSGI utility libraries. Flask wraps Werkzeug, allowing it to take care of the WSGI intricacies while also offering extra structure and patterns for creating powerful applications.


1 Answers

Try something like this instead (untested):

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)

What you do is simply create a different logger so they don't mix up, that should fix the problems you're having :)

like image 52
Wolph Avatar answered Sep 23 '22 20:09

Wolph