我在网上搜索,但没有得到具体答案或"how to use log rotation with Gunicorn?"
的示例。如果有人提供示例,那就太好了。
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
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
}
每天轮换