7

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 -
rdodev
  • 3,164
  • 3
  • 26
  • 34
Raj
  • 22,346
  • 14
  • 99
  • 142

2 Answers2

7

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?

loopbackbee
  • 21,962
  • 10
  • 62
  • 97
  • how do you differentiate development/production in Flask? – Raj Mar 11 '15 at 12:09
  • @emaillenin as you'd differentiate them in any other piece of software (which is a very broad question). Basically, if it's exposed to anyone but developers and internal quality control, it's production, I'd say. – loopbackbee Mar 11 '15 at 13:25
4

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)
iJade
  • 23,144
  • 56
  • 154
  • 243