skamsie
skamsie

Reputation: 2726

Flask: Can't get gunicorn to log everything to file

There are several similar questions on SO, I've red them all but somehow I can't make my gunicorn log files show all information that I am interested in:

so, considering these 3 relevant files:

run.py

from app import app


if __name__ == '__main__':
    app.run(
        passthrough_errors=False,
        threaded=True
    )

app/__init__.py

import logging
from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config.from_object('config')

handler = logging.StreamHandler()
handler.setLevel(logging.INFO)

app.logger.addHandler(handler)
app.logger.level = logging.DEBUG
app.logger.handlers.extend(logging.getLogger("gunicorn.info").handlers)

db = SQLAlchemy(app)

import views, models

views.py

@app.before_request
def before_request():
    app.logger.info('Body: %s', request.get_data())
    ...
    ...

Command to run the app:

gunicorn -w 4 run:app --access-logfile /Users/adimian/bla.log \ 
--log-file /Users/mondrian/bla.log --error-log /Users/mondrian/error.log

The problem: the body of the request does not go to any logfile, but instead it is printed in the console. Everything else goes to the correct file, but not the body...

I am also not interested in providing a log file path in the config, but instead it should work by passing it as a command line argument to gunicorn, as per my example above.

Upvotes: 2

Views: 3594

Answers (1)

JRG
JRG

Reputation: 2135

Did you check if the handlers are actually empty? GUnicorn's logger is gunicorn.error, not gunicorn.info. I do something similar in my application:

import logging
from flask import Flask

app = Flask(__name__)

root = logging.getLogger()
root.handlers = logging.getLogger('gunicorn.error').handlers
root.setLevel(logging.INFO)
...

This works fine for me; all logging is sent to the gunicorn error log.

Upvotes: 2

Related Questions