How to create one uwsgi log file per day?

前端 未结 4 742
小鲜肉
小鲜肉 2021-02-14 13:11

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 pre

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-14 13:36

    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.

提交回复
热议问题