wandermonk
wandermonk

Reputation: 7346

Logging in flask with gunicorn

I am a developer transitioning from Java to Python.

I have a flask application which is deployed on gunicorn. For debugging the application I have used loggers as below

#For logging
from logging.handlers import RotatingFileHandler
import logging
from logging import Formatter


logger = logging.getLogger('backend-logging')
file_handler = RotatingFileHandler('test.log', maxBytes=10000, backupCount=1)
handler = logging.StreamHandler()
file_handler.setFormatter(Formatter(
    '%(asctime)s %(levelname)s: %(message)s '
    '[in %(pathname)s:%(lineno)d]'
))
handler.setFormatter(Formatter(
    '%(asctime)s %(levelname)s: %(message)s '
    '[in %(pathname)s:%(lineno)d]'
))
logger.addHandler(file_handler)
logger.addHandler(handler)
logger.setLevel(logging.INFO)
#EOL

In each file the above code snippet is used to log the error,debug,info messages.

logger.info('creating user with the data %s',json.dumps(data))

Here is my wsgi application

from server import app

if __name__ == "__main__":
    app.run(debug=True)

When i am starting my application using the below command it is not printing the loggers.

gunicorn3 --bind 0.0.0.0:5000 --log-level DEBUG wsgi:app

Could someone help me if this the right way to log application logs.

Upvotes: 2

Views: 2516

Answers (1)

Deepak Sood
Deepak Sood

Reputation: 405

You have to specify --access-logfile and --error-logfile as '-', while running your Gunicorn. This tells gunicorn to send error and access log to stdout stream.

Example

# make sure to give --access-logfile and --error-logfile as '-' to make gunicorn send logs to stdout
gunicorn app:app -b 0.0.0.0:5000 --workers 2 -k gevent --timeout 300 --worker-connections 1000 --max-requests 1000000 --limit-request-line 8190 --access-logfile '-' --error-logfile '-'

Maybe this can help. Its using python's dictConfig to set up logging.

Setting up logging for Gunicorn in a Flask application using Python's dictConfig

Upvotes: 4

Related Questions