Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create one uwsgi log file per day?

I use uwsgi with the parameter --daemonize /logs/uwsgi.log

This file is however becoming large and I would like to split it into smaller pieces. One per day would be preferable. I have done that for nginx where it was easy to specify the log file name using date variables. Is it possible to do the same for uwsgi?

Something like:

[uwsgi]
daemonize=/logs/uwsgi-$year-$month-$day.log

Or is there another way to get the same result, without too much involvement of other processes?

like image 667
Markus Johansson Avatar asked Feb 05 '15 11:02

Markus Johansson


2 Answers

uWSGI by itself can only "split by size", with the --log-maxsize option.

Time-based approaches are using classic logrotate or apache rotatelogs (http://httpd.apache.org/docs/2.2/programs/rotatelogs.html) that you can combine with uWSGI logpipe plugin.

Finally you can have an nginx like behaviour triggering a reload at midnight of the uWSGI instance (you can even use the embedded cron facility):

[uwsgi]
daemonize = /logs/uwsgi-@(exec://date +%%Y-%%m-%%d).log
log-reopen = true
like image 114
roberto Avatar answered Nov 06 '22 01:11

roberto


Reloading uwsgi every hour felt heavy-handed and I wanted a more efficient solution. uWSGI has a built in rotation mechanism, however (as of now) can only be automatically triggered when the log reaches a certain size. uWSGI supports forced log rotation via fifo, which led me to the following solution that avoids a reload and is entirely handled within uwsgi. The following ini should work in uWSGI 1.9.11+:

[uwsgi]
# Directory for demo purposes
uwsgi-directory = /var/uwsgi
master-fifo = %(uwsgi-directory)/uwsgi.fifo
logto = %(uwsgi-directory)/logs/uwsgi.log

# Destination for rotated log
log-backupname = %(uwsgi-directory)/logs/uwsgi.log.rotated

log-master = true
log-reopen = true

# Cron to trigger log rotation each hour
cron2 = hour=-1,minute=0,unique=1 echo L > %(master-fifo) && sleep 5 && mv %(log-backupname) %(logto).$(/bin/date -u -d '-1 hour' +%%Y-%%m-%%d-%%H)

Every hour, at minute zero, uwsgi will write "L" to the uwsgi fifo (triggering log rotation). It will then sleep for a few seconds before moving the rotated log to have the desired date format in the filename. The sleep may be extraneous, however I wanted to ensure that uwsgi had time to rotate the log. Additionally, the cron explicitly triggers on minute zero to avoid log rotation if uwsgi is restarted at any other time during the hour.

This could probably be used with older versions of uWSGI by adapting for the older style uwsgi cron option or using crontab.

like image 27
johanno Avatar answered Nov 06 '22 02:11

johanno