Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform log rotation with Gunicorn?

Tags:

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.

like image 807
neel Avatar asked Apr 05 '16 10:04

neel


People also ask

How do you rotate logs with logrotate?

In the default behavior, the rotation process beings by logrotate renaming the active log file into a different name. Then, it creates a new log file with the same name. Finally, it invokes other logic such as compress and mail to complete the rotation process.

How do I view Gunicorn error logs?

To watch the logs in the console you need to use the option --log-file=- . In version 19.2, Gunicorn logs to the console by default again.

How do I force a rotation in syslog?

If you want to rotate /var/log/syslog it needs to be listed in a logrotate config file somewhere, and you just run logrotate . If it rotated recently, then logrotate -f to force it to do it again. So, you need that in a file, normally either /etc/logrotate. conf or as a file snippet in /etc/logrotate.

How does log rotate work?

Each time a new log file is started, the numbers in the file names of old logfiles are increased by one, so the files "rotate" through the numbers (thus the name "log rotation"). Old logfiles whose number exceeds a threshold can then be deleted or archived off-line to save space.


2 Answers

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 
like image 74
pawamoy Avatar answered Sep 19 '22 01:09

pawamoy


If you do not want to bother with logrotare you can use a full python/gunicorn solution using python logging facilities.

Create a file named log.conf with the content below. This will rotate the error log file and access log file daily and will keep the logs for 90 days. Log level is set to INFO.

Than, start gunicorn adding a command line parameter --log-config log.conf. Remove the --access-logfile, --error-logfile and --log-level parameters, as the log.conf will take care of everything.

More info on how to configure the logger is available in the Python documentation.

Here below is the content of log.conf. For another example look at gunicorn source code.

HTH

[loggers] keys=root, gunicorn.error, gunicorn.access  [handlers] keys=console, error_file, access_file  [formatters] keys=generic, access  [logger_root] level=INFO handlers=console  [logger_gunicorn.error] level=INFO handlers=error_file propagate=1 qualname=gunicorn.error  [logger_gunicorn.access] level=INFO handlers=access_file propagate=0 qualname=gunicorn.access  [handler_console] class=StreamHandler formatter=generic args=(sys.stdout, )  [handler_error_file] class=logging.handlers.TimedRotatingFileHandler formatter=generic args=('/var/log/gunicorn/gunicorn-error.log', 'midnight', 1, 90, 'utf-8')  [handler_access_file] class=logging.handlers.TimedRotatingFileHandler formatter=access args=('/var/log/gunicorn/gunicorn-access.log', 'midnight', 1, 90, 'utf-8')  [formatter_generic] format=%(asctime)s [%(process)d] [%(levelname)s] %(message)s datefmt=%Y-%m-%d %H:%M:%S class=logging.Formatter  [formatter_access] format=%(message)s class=logging.Formatter 
like image 36
Charlie Avatar answered Sep 20 '22 01:09

Charlie