Log rotation with Gunicorn

本小妞迷上赌 提交于 2019-12-06 19:25:42

问题


I searched through the net but didn't get the concrete answer or example of "how to use log rotation with Gunicorn?".
It would be great if someone provide an example.


回答1:


Gunicorn's documentation says you can setup log rotation with logrotate (a linux command):

Logs can be automatically rotated and compressed using logrotate.

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

So I guess Gunicorn provides itself no way to rotate log.

Here is an example of my configuration file, placed in /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
}

Rotate monthly, add -YEAR-MONTH to rotated files, keep 10000 rotated files (see man logrotate).

The paths at first line are declared in my gunicorn_start script, something like:

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



回答2:


doc link : 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)

so you can write the configuration file like this:

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

rotate everyday



来源:https://stackoverflow.com/questions/36424335/log-rotation-with-gunicorn

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!