How to create one uwsgi log file per day?

前端 未结 4 741
小鲜肉
小鲜肉 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: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.

    0 讨论(0)
  • 2021-02-14 13:39
    [uwsgi]
    
    wsgi-file = bean/wsgi.py
    
    # 重启时间
    # harakiri = 10
    
    # 主进程
    master = true
    
    # 4个进程/workers
    processes = 4
    
    # 两个核心
    threads = 2
    
    # 开启线程
    enable-threads = true
    
    # 单个进程最大请求数
    max-requests = 5000
    
    # 解包缓冲区大小
    buffer-size = 65536
    
    reload-mercy = 8
    
    stats = 127.0.0.1:5000
    
    pidfile = /var/run/itpserver.pid
    
    vacuum = true
    
    disable-logging = true
    
    logto = /opt/logs/uwsgi.log
    
    touch-logreopen = /opt/logs/.touchforlogrotat
    
    

    And

    #!/bin/bash
    
    if [ ${ITM_CONFIG}=="test" ]
    then
    LOGDIR="/home/logs"
    else
    LOGDIR="/opt/logs"
    fi
    
    DATE=`date -d "yesterday" +"%Y-%m-%d"`
    
    BACKUPCOUNT="7"
    
    DELDATE=`date -d "${BACKUPCOUNT} day ago" +"%Y-%m-%d"`
    
    mv ${LOGDIR}/uwsgi.log   ${LOGDIR}/uwsgi.${DATE}.log
    
    rm -f ${LOGDIR}/uwsgi.${DELDATE}.log
    
    touch ${LOGDIR}/.touchforlogrotat
    

    And add crontab with this sh:

    @daily sh /home/share/logbackups.sh # uwsgilogrotat
    

    A friendlier way

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-14 13:44

    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
    
    0 讨论(0)
提交回复
热议问题