uWsgi Daily Log Rotation Without Service Restart

July 23, 2018

NOTE: Rotate Log Daily For uWSGI With Logrotate works pretty well if you don’t mind daily uwsgi restart.

I didn’t use logto or logto2 because I want to separate access log from other log (using req-logger and logger).

# logto = /var/log/uwsgi/myapp.log
# logto2 = /var/log/uwsgi/myapp.log
req-logger = file:/var/log/uwsgi/myapp-req.log
logger = file:/var/log/uwsgi/myapp.log

touch-logreopen allow reopening of log file by touching a specific file. Sadly, it doesn’t work with req-logger. log-reopen doesn’t seems to work or has any effects as well.

log-maxsize allow changing of logfile when specific size is reached (it doesn’t allow daily rotate though). The main drawback is that there is no mechanism to delete or compress old logs.

The only reasonable solution found without daily restart of uwsgi is to use copytruncate for logrotate. The drawback is some log entries might be lost during daily transition (depending on how long the copy and truncate operation takes, and how busy is your requests).

Sample of my uwsgi config.

[uwsgi]
master = true
processes = 2
enable-threads = true
socket = /tmp/uwsgi-myapp.sock
module = run:app
home = /code/python/myapp/env
chmod-socket = 666
pythonpath = /code/python/myapp
# touch-reload = /code/python/myapp/run.py
# touch-logreopen = /var/log/uwsgi/touch-logreopen
# logto = /var/log/uwsgi/myapp.log
# logto2 = /var/log/uwsgi/myapp.log
# log-reopen = true
req-logger = file:/var/log/uwsgi/myapp-req.log
logger = file:/var/log/uwsgi/myapp.log
uid = www-data
gid = www-data

Sample logrotate setup.

cd /etc/logrotate.d
sudo nano uwsgi
/var/log/uwsgi/*.log {
        daily
        missingok
        rotate 14
        compress
        delaycompress
        notifempty
        copytruncate
        sharedscripts
}
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.