How to create one uwsgi log file per day?

前端 未结 4 732
小鲜肉
小鲜肉 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条回答
  •  孤城傲影
    2021-02-14 13:42

    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.

提交回复
热议问题