Gunicorn 和主管背后的 Flask - 记录所有请求和响应

Flask behind gunicorn and supervisor - log all requests and responses

我在gunicorn和supervisor之后继承了一个flask server 运行。在我想查看的日志文件中:

我有多个 gunicorn 工人。我的 gunicorn.conf.py 看起来像这样:

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

gunicorn.conf 对于 supervisor 看起来像这样:

[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

对于 loglevel=info,我希望在 gunicorn_stdout.loggunicorn_stderr.log 中看到请求和响应,但没有骰子。

我已经实现了 this 用于日志记录,并且它有效,但是必须手动发送每个请求和响应 logger.info 似乎很疯狂。

有没有 setting somewhere here 这会自动发生的地方?

如果是,我应该把它放在哪里?

此外,我假设所有工作人员都写入同一个日志....

编辑: 这是我通过接受的答案添加到 gunicorn.conf.py 的内容:

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"'

loglevel 配置设置仅影响 error 日志,因此更改其值不会帮助您记录成功的请求和响应。

相反,尝试在 gunicorn.conf.py 配置文件中设置 accesslog (to enable access logs) and access_log_format