How to create one uwsgi log file per day?

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.


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

Based on roberto's answer here is the configuration that will rotate logs. It will keep up to 14 log files. Daily rotation at 3:15.

[uwsgi]
set-placeholder = log_dir=/var/log
set-placeholder = log_prefix=myservice-
set-placeholder = log_num=14
pidfile = /var/run/uwsgi-myservice.pid
logto = %(log_dir)/%(log_prefix)@(exec://date +%%Y-%%m-%%d).log
log-reopen = true
unique-cron = 15 3 -1 -1 -1 { sleep 66 && kill -HUP $(cat %(pidfile)) && ls -tp %(log_dir)/%(log_prefix)* | grep -v '/$' | tail -n +%(log_num)  | xargs -d '\n' -r rm --; } &

The sleep is needed because after reload uwsgi will execute cronjob again because it would match current time. So we need a sleep for more than 60 seconds before reloading. It also reloads configuration file on every rotation, such behavior might be not desired.

Why one would need such a hack? Well, in my case I don't have access to properly configure logging in the system, but I have permission to change uwsgi config.