Gunicorn: Log messages from Flask's app.logger

Created on 19 Jul 2012  Â·  11Comments  Â·  Source: benoitc/gunicorn

Messages from Flask's logger such as app.logger.info("This is a message") are not saved in the gunicorn log files.

Most helpful comment

Flask does not log messages in production mode by default because it does not make any assumptions about your environment (http://flask.pocoo.org/docs/errorhandling/). To get Flask's app.logger to log messages via gunicorn, you'll have to add a logging handler. For example, here's a logger to gunicorn's stderr:

import logging
import flask

app = flask.Flask(__name__)

@app.before_first_request
def setup_logging():
    if not app.debug:
        # In production mode, add log handler to sys.stderr.
        app.logger.addHandler(logging.StreamHandler())
        app.logger.setLevel(logging.INFO)

This is not an issue with gunicorn and can be closed.

All 11 comments

Flask does not log messages in production mode by default because it does not make any assumptions about your environment (http://flask.pocoo.org/docs/errorhandling/). To get Flask's app.logger to log messages via gunicorn, you'll have to add a logging handler. For example, here's a logger to gunicorn's stderr:

import logging
import flask

app = flask.Flask(__name__)

@app.before_first_request
def setup_logging():
    if not app.debug:
        # In production mode, add log handler to sys.stderr.
        app.logger.addHandler(logging.StreamHandler())
        app.logger.setLevel(logging.INFO)

This is not an issue with gunicorn and can be closed.

closed per request. Thanks!

Does gunicorn automatically pick up the log from STDERR?

@benoitc
How do I force gunicorn to pick log from STDERR?

The -R flag might be what you're looking for.

http://gunicorn-docs.readthedocs.org/en/latest/settings.html#enable-stdio-inheritance

-R did not do it for me.

Below is my setup

@app.route('/')
def index():
    console = logging.StreamHandler()
    log = logging.getLogger("asdasd")
    log.addHandler(console)
    log.setLevel(logging.DEBUG)
    log.error("Something")
    print >> sys.stderr, "Another thing"
    return 'ok'

Here is how I run it

gunicorn --access-logfile - --log-file /mnt/log/test.log --bind 0.0.0.0:8080 --workers 2 --worker-class gevent --log-level -D debug server:app

I get the gunicorn logs in /mnt/log/test.log but none of my application logs.

Okay. I may misunderstand how that works, unless it's a simple matter of
having to flush stderr manually (IIRC this is because it becomes buffered
when gunicorn forks, maybe?)

But it's definitely much easier to control where your log messages go if
you use the python loggers instead of stdio.
On Jun 26, 2014 1:39 PM, "Yousuf Fauzan" [email protected] wrote:

-R did not do it for me.

Below is my setup

@app.route('/')
def index():
console = logging.StreamHandler()
log = logging.getLogger("asdasd")
log.addHandler(console)
log.setLevel(logging.DEBUG)
log.error("Something")
print >> sys.stderr, "Another thing"
return 'ok'

Here is how I run it

gunicorn --access-logfile - --log-file /mnt/log/test.log --bind 0.0.0.0:8080 --workers 2 --worker-class gevent --log-level debug server:app

I get the gunicorn logs in /mnt/log/test.log but none of my application
logs.

—
Reply to this email directly or view it on GitHub
https://github.com/benoitc/gunicorn/issues/379#issuecomment-47276949.

I think I found the problem. Here is an entry in the changelog for 19.00

fix logging: don’t try to redirect stdout/stderr to the logfile.

I just tried with v18 and the logs from StreamHandler are getting routed to gunicorn logs. Any idea how to fix this?
@benoitc @tilgovi

@spicavigo gunicorn won't redirect the stderr to the logfile automatically. The fix was to remove the hack like it was discussed in #591 . Did you try --error-logfile=- which display the logs on the console?

If you want to directly return error to the error file you can eventually print to environ['wsgi.errors'] or log it: ee08ac86441e36c3433849b79b3839d1425647fd .

Thanks @benoitc

I am now logging to a file instead of stderr. Its just that the change happened in between my dev and so it totally tripped me. I was pip install gunicorn and suddenly the stderr redirect stopped working and I didn't realize it was because of version change and not due to bugs in my code :)

@benoitc Is it still possible to log stderr to the logfile somehow?

Was this page helpful?
0 / 5 - 0 ratings