Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log messages from Flask's app.logger #379

Closed
nickhs opened this issue Jul 19, 2012 · 11 comments
Closed

Log messages from Flask's app.logger #379

nickhs opened this issue Jul 19, 2012 · 11 comments

Comments

@nickhs
Copy link

nickhs commented Jul 19, 2012

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

@rduplain
Copy link
Contributor

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.

@benoitc
Copy link
Owner

benoitc commented Aug 26, 2012

closed per request. Thanks!

@benoitc benoitc closed this as completed Aug 26, 2012
@maxcountryman
Copy link

Does gunicorn automatically pick up the log from STDERR?

@spicavigo
Copy link

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

@tilgovi
Copy link
Collaborator

tilgovi commented Jun 26, 2014

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

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

@spicavigo
Copy link

-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.

@tilgovi
Copy link
Collaborator

tilgovi commented Jun 26, 2014

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
#379 (comment).

@spicavigo
Copy link

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

@benoitc
Copy link
Owner

benoitc commented Jun 26, 2014

@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: ee08ac8 .

@spicavigo
Copy link

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 :)

@tlatorre-uchicago
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants