How to perform log rotation with Gunicorn?

neel picture neel · Apr 5, 2016 · Viewed 8.6k times · Source

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.

Answer

pawamoy picture pawamoy · Apr 21, 2017

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