Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Flask writes access log to STDERR

Flask is writing access logs to STDERR stream instead of STDOUT. How to change this configuration so that access logs go to STDOUT and application errors to STDERR?

open-cricket [master] python3 flaskr.py > stdout.log 2> stderr.log &
[1] 11929

open-cricket [master] tail -f stderr.log
 * Running on http://127.0.0.1:9001/
127.0.0.1 - - [11/Mar/2015 16:23:25] "GET /?search=Sachin+Tendulkar+stats HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2015 16:23:25] "GET /favicon.ico HTTP/1.1" 404 -
like image 520
Lenin Raj Rajasekaran Avatar asked Mar 11 '15 10:03

Lenin Raj Rajasekaran


2 Answers

I'll assume you're using the flask development server.

Flask's development server is based on werkzeug, whose WSGIRequestHandler is, in turn, based in the BaseHTTPServer on the standard lib.

As you'll notice, WSGIRequestHandler overrides the logging methods, log_request, log_error and log_message, to use it's own logging.Logger - so you can simply override it as you wish, in the spirit of IJade's answer.

If you go down that route, I think it'd be cleaner to add your own FileHandler instead, and split the stdout and stderr output using a filter

Note, however, that all this is very implementation specific - there's really no proper interface to do what you want.

Although it's tangential to your actual question, I feel I really must ask - why are you worried about what goes into each log on a development server?

If you're bumping into this kind of problem, shouldn't you be running a real web server already?

like image 111
loopbackbee Avatar answered Oct 02 '22 19:10

loopbackbee


Here is what you got to do. Import logging and set the level which you need to log.

import logging
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
like image 44
iJade Avatar answered Oct 02 '22 20:10

iJade