天天看點

普通日志與uwsgi日志的轉儲

1. 使用linux中的logrotate轉儲

vim /etc/logrotate.conf

/data/log/uwsgi.log {
daily
rotate 10
dateext
missingok
notifempty
copytruncate
nocompress
sharedscripts
}
           

daily:每天轉儲一次,該參數還有 weekly/monthly/yearly 值。

rotate 10:最多保留 10 個轉儲日志檔案。

dateext:以日期格式作為轉儲日志檔案名字尾。

missingok:如果源日志不存在,忽略錯誤。

notifempty:如果源日志為空,不執行轉儲。

copytruncate:将源日志内容截取出來,用其内容建立轉儲檔案。

nocompress:不壓縮。

sharedscripts:對一批日志檔案使用同一個腳本一次執行,而不是每個單獨執行。

1. uwsgi日志轉儲

daemonize=/data/log/uwsgi/uwsgi.log,uwsgi的日志轉儲經過驗證使用logrotate是不可行的,uwsgi本身提供根據檔案大小來rotate的功能,如果想要通過日期每天轉儲,首先需要使用touch-logreopen來設定一個監聽對象:

daemonize=/data/log/uwsgi/uwsgi.log
# 使得uwsgi.log檔案被轉存後能繼續在uwsgi.log檔案中寫入日志,且不會中斷目前程式的執行
touch-logreopen =/data/log/uwsgi/.touchforlogrotat
           

在建立腳本touchforlogrotat.sh

#!/bin/bash

DIR=`echo $(cd "$(dirname "$0")"; pwd)`       #擷取目前目錄
LOGDIR="/data/log/uwsgi/"                   #log目錄

sourcelogpath="${LOGDIR}uwsgi.log"            #log源位址
touchfile="${LOGDIR}.touchforlogrotat"       #需要touch的檔案

DATE=`date -d "yesterday" +"%Y%m%d"`
destlogpath="${LOGDIR}uwsgi-${DATE}.log"     #重命名後的檔案
mv $sourcelogpath $destlogpath

touch $touchfile                            # 更新檔案時間戳
           

當監聽對象touch-logreopen所指向的檔案被touch,時間戳改變後,uwsgi會重新打開uwsgi.log檔案進行寫入,且不會中斷目前程式的執行。如果沒有touch-logreopen這個監聽對象,是無法對uwsgi.log進行轉儲的。通過crontab設定定時任務

輸入以下行,表示每天0點進行轉儲。