如何使用Gunicorn执行日志旋转?

问题描述 投票:13回答:2

我在网上搜索,但没有得到具体答案或"how to use log rotation with Gunicorn?"的示例。如果有人提供示例,那就太好了。

logging gunicorn logrotate
2个回答
12
投票

Gunicorn的文档说,您可以使用logrotate(Linux命令)设置日志轮换:

可以使用logrotate自动旋转和压缩日志。

文档链接:http://docs.gunicorn.org/en/latest/install.html?highlight=logrotate#debian-gnu-linux

所以我想Gunicorn无法提供旋转日志的方式。

这是我的配置文件的示例,放置在/etc/logrotate.d/my_app中:

/path/to/my/logs/gunicorn-access.log /path/to/my/logs/gunicorn-error.log {
    monthly
    dateext
    dateformat -%Y-%m
    dateyesterday
    rotate 10000
}

每月轮换,将-YEAR-MONTH添加到轮换的文件中,保留10000个轮换的文件(请参阅man logrotate

第一行的路径在我的gunicorn_start脚本中声明,类似于:

/my/virtualenv/bin/gunicorn OPTIONS \
    --access-logfile /path/to/my/logs/gunicorn-access.log \
    --error-logfile /path/to/my/logs/gunicorn-error.log

4
投票

doc链接:http://docs.gunicorn.org/en/latest/install.html?highlight=logrotate#debian-gnu-linux

Logging
Logging can be configured by using various flags detailed in the configuration documentation or by creating a logging configuration file. Send the USR1 signal to rotate logs if you are using the logrotate utility:
kill -USR1 $(cat /var/run/gunicorn.pid)

所以您可以这样编写配置文件:

/yourpath/log/gunicorn.* {
daily
rotate 30
compress
dateext
dateformat .%Y-%m-%d
notifempty
sharedscripts
postrotate
    kill -USR1 $(cat /yourpath/run/gunicorn.pid)
endscript
}

每天轮换

© www.soinside.com 2019 - 2024. All rights reserved.