Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Flask and native Python logging?

The trouble with Flask logging (i.e., app.logger.info(...), etc.) is that sub modules don't use it, so it seems to me that the only way to globally configure the app's logging is via the underlying python logging mechanism: e.g.,

logging.config.fileConfig('config/logging.conf')

But this doesn't configure Flask's default handlers, so for example, the HTTP logging is not configured. So I get mixed logs:

2015-07-28 14:57:47,320 [main.py][INFO] Starting...           # Python log
[2015-07-28 14:58:49] "GET /demo HTTP/1.1" 200 956 0.318825   # Flask log

Can anyone suggest the standard practice for globally configuring logging via a configuration file (i.e., so that I can easily configure individual packages).

ALSO, I cant seem to remove the HTTP logging (GET, etc.) to stderr (which I presume come from werkzeug); setting logging.getLogger('werkzeug').setLevel() only affects the werkzeug logs that are not related to HTTP logging.

Thanks.

like image 304
rsb Avatar asked Jul 28 '15 19:07

rsb


1 Answers

I'm pretty sure there is a piece about this in the Flask docs: https://flask.palletsprojects.com/en/1.1.x/logging/#other-libraries

It basically suggests to add the handlers of other libraries to the root of Flask. This is the example they give:

from flask.logging import default_handler

root = logging.getLogger()
root.addHandler(default_handler)
root.addHandler(mail_handler)
like image 92
Gijs Wobben Avatar answered Oct 24 '22 19:10

Gijs Wobben