问题
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