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

Comments

Projects
None yet
7 participants
@nickhs

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

This comment has been minimized.

Show comment
Hide comment
@rduplain

rduplain Aug 21, 2012

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.

rduplain commented Aug 21, 2012

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

This comment has been minimized.

Show comment
Hide comment
@benoitc

benoitc Aug 26, 2012

Owner

closed per request. Thanks!

Owner

benoitc commented Aug 26, 2012

closed per request. Thanks!

@benoitc benoitc closed this Aug 26, 2012

@maxcountryman

This comment has been minimized.

Show comment
Hide comment
@maxcountryman

maxcountryman Sep 7, 2012

Does gunicorn automatically pick up the log from STDERR?

maxcountryman commented Sep 7, 2012

Does gunicorn automatically pick up the log from STDERR?

@spicavigo

This comment has been minimized.

Show comment
Hide comment
@spicavigo

spicavigo Jun 26, 2014

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

spicavigo commented Jun 26, 2014

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

@tilgovi

This comment has been minimized.

Show comment
Hide comment
@tilgovi
Collaborator

tilgovi commented Jun 26, 2014

@spicavigo

This comment has been minimized.

Show comment
Hide comment
@spicavigo

spicavigo Jun 26, 2014

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

spicavigo commented Jun 26, 2014

-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

This comment has been minimized.

Show comment
Hide comment
@tilgovi

tilgovi Jun 26, 2014

Collaborator

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" notifications@github.com 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).

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" notifications@github.com 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

This comment has been minimized.

Show comment
Hide comment
@spicavigo

spicavigo Jun 26, 2014

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 commented Jun 26, 2014

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

This comment has been minimized.

Show comment
Hide comment
@benoitc

benoitc Jun 26, 2014

Owner

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

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

This comment has been minimized.

Show comment
Hide comment
@spicavigo

spicavigo Jun 26, 2014

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

spicavigo commented Jun 26, 2014

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

This comment has been minimized.

Show comment
Hide comment
@tlatorre-uchicago

tlatorre-uchicago Feb 13, 2015

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

tlatorre-uchicago commented Feb 13, 2015

@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