Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask behind gunicorn and supervisor - log all requests and responses

I have inherited a flask server running behind gunicorn and supervisor. In a log file I want to see:

  • All incoming requests
  • All outgoing responses

I have multiple gunicorn workers. My gunicorn.conf.py looks like this:

import multiprocessing

bind = "0.0.0.0:8000"

workers = multiprocessing.cpu_count() * 2 + 1
worker_class = 'gevent'

max_requests = 1000
timeout = 30
keep_alive = 2

preload = True

and gunicorn.conf for supervisor looks like this:

[program:gunicorn]
command=/opt/anaconda/bin/gunicorn manage:app -c /etc/config/gunicorn.conf.py
directory=/root/ourthing/web
environment=PYTHONPATH=/root/ourthing/web
user=root
autorestart=true
stdout_logfile=/opt/logs/gunicorn_stdout.log
stderr_logfile=/opt/logs/gunicorn_stderr.log
loglevel=info
priority=400

With loglevel=info, I expected to see requests and responses in gunicorn_stdout.log and gunicorn_stderr.log, but no dice.

I have implemented this for logging, and it works, but to have to manually send every request and response with logger.info seems insane.

Is there a setting somewhere here where this will just happen automagically?

If so, where do I put it?

Also, I assume all the workers write to the same log....

EDIT: Here is what I added to gunicorn.conf.py via the accepted answer:

accesslog = '/root/logs/accesslog.log'
loglevel = 'debug'
access_log_format = '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"'
like image 544
scharfmn Avatar asked Aug 04 '15 16:08

scharfmn


People also ask

How does gunicorn handle multiple requests?

Gunicorn is based on the pre-fork worker model. This means that there is a central master process that manages a set of worker processes. The master never knows anything about individual clients. All requests and responses are handled completely by worker processes.

What is the difference between gunicorn and flask?

Flask is a Python-based microframework that is popular with web developers, given its lightweight nature and ease of use. This tutorial will focus on deploying a Flask app to App Platform using gunicorn. Gunicorn is a Python WSGI HTTP Server that uses a pre-fork worker model.

How do I get gunicorn logs?

In version 19.0, Gunicorn doesn't log by default in the console. To watch the logs in the console you need to use the option --log-file=- . In version 19.2, Gunicorn logs to the console by default again.

Where does gunicorn log by default?

errorlog. The Error log file to write to. Using '-' for FILE makes gunicorn log to stderr. Changed in version 19.2: Log to stderr by default.


1 Answers

The loglevel configuration setting only affects the error log, so changing its value won't help you log successful requests and responses.

Instead, try setting accesslog (to enable access logs) and access_log_format in your gunicorn.conf.py configuration file.

like image 62
Jacob Budin Avatar answered Sep 21 '22 14:09

Jacob Budin