利用Linux自帶的logrotate管理日志


日常運維中,經常要對各類日志進行管理,清理,監控,尤其是因為應用bug,在1小時內就能寫幾十個G日志,導致磁盤爆滿,系統掛掉。

nohup.out,access.log,catalina.out

本文簡單介紹利用Linux自帶的logrotate來對操作系統中各類日志進行管理。

1、logrotate簡介

The logrotate utility is designed to simplify the administration of log files on a system which generates a lot of log files. Logrotate allows for the automatic rotation compression, removal and mailing of log files. Logrotate can be set to handle a log file daily, weekly, monthly or when the log file gets to a certain size.

為了使用它,主要有兩個地方需要修改一下:一個是/etc/logrotate.conf,另一個是/etc/logrotate.d/下面的文件。

你既可以在logrotate.conf中直接定義如何處理你的log文件,也可以在/logrotate.d/下面針對自己的log新建一個對應的文件來定義處理log的行為,推薦在目錄 /logrotate.d/ 下面創建自己的文件來對個性化的日志進行處理。

logrotate定義了如何處理日志,而它本身則是被crond定時調用的。

我使用的一個生產實例:

/usr/local/nginx/logs/*.log {
    create 0644 root root
    daily
    rotate 2
    missingok
    copytruncate
    ifempty
    compress
    noolddir
}

上述內容保存到nginxlog文件,存放到目錄:/etc/logrotate.d/nginxlog
設置權限:owner=root group=root mode=0644

測試配置是否正確:

lograte -d /etc/logrotate.d/nginxlog

2、logrotate配置參數

logrotate 全局配置文件: /etc/logrotate.conf

配置參數 功能說明
compress 通過gzip 壓縮轉儲以后的日志
nocompress 不需要壓縮時,用這個參數
copytruncate 用於還在打開中的日志文件,把當前日志備份並截斷;是先拷貝再清空的方式,拷貝和清空之間有一個時間差,可能會丟失部分日志數據。
nocopytruncate 備份日志文件但是不截斷
create mode owner group  轉儲文件,使用指定的文件模式創建新的日志文件。輪轉時指定創建新文件的屬性,如create 0777 nobody nobody
nocreate 不建立新的日志文件
delaycompress 和 compress 一起使用時,轉儲的日志文件到下一次轉儲時才壓縮
nodelaycompress 覆蓋 delaycompress 選項,轉儲同時壓縮
errors address  專儲時的錯誤信息發送到指定的Email 地址
ifempty 即使是空文件也轉儲,這個是 logrotate 的缺省選項。
notifempty 如果是空文件的話,不轉儲
mail address  把轉儲的日志文件發送到指定的E-mail 地址
nomail 轉儲時不發送日志文件
olddir directory 轉儲后的日志文件放入指定的目錄,必須和當前日志文件在同一個文件系統
noolddir 轉儲后的日志文件和當前日志文件放在同一個目錄下

prerotate/endscript

在logrotate轉儲之前需要執行的指令,例如修改文件的屬性等動作;這兩個關鍵字必須單獨成行;
postrotate/endscript 在logrotate轉儲之后需要執行的指令,例如重新啟動 (kill -HUP) 某個服務!必須獨立成行;
daily 指定轉儲周期為每天
weekly 指定轉儲周期為每周
monthly 指定轉儲周期為每月
rotate count 指定日志文件刪除之前轉儲的個數,0 指沒有備份,5 指保留5個備份
tabootext [+] list 讓logrotate  不轉儲指定擴展名的文件,缺省的擴展名是:.rpm-orig, .rpmsave, v, 和 ~ 
size size當日志文件到達指定的大小時才轉儲,Size 可以指定 bytes (缺省)以及KB (sizek)或者MB (sizem).
missingok 如果日志丟失,不報錯繼續滾動下一個日志
notifempty 當日志文件為空時,不進行輪轉
sharedscripts 運行postrotate腳本,作用是在所有日志都輪轉后統一執行一次腳本。如果沒有配置這個,那么每個日志輪轉后都會執行一次腳本
dateext 使用當期日期作為命名格式
dateformat .%s  配合dateext使用,緊跟在下一行出現,定義文件切割后的文件名,必須配合dateext使用,只支持 %Y %m %d %s 這四個參數
size(或minsize) log-size 當日志文件到達指定的大小時才轉儲,log-size能指定bytes(缺省)及KB (sizek)或MB(sizem).

說明:

當日志文件 >= log-size 的時候就轉儲。
以下為合法格式:(其他格式的單位大小寫沒有試過)
size = 5 或 size 5 (>= 5 個字節就轉儲)
size = 100k 或 size 100k
size = 100M 或 size 100M

實例:

/home/deploy/apps/production.log {
missingok
copytruncate
rotate 10
notifempty
sharedscripts
dateext
dateformat -%Y-%m-%d-%s
size=10M
postrotate
mv /home/deploy/apps/production.log-* /data1/log/rails
gzip /data1/log/rails/production.log-*
endscript
}

問題:rotate和maxage的區別是什么?
它們都是用來控制保存多少日志文件的,區別在於 rotate 是以個數為單位的,而 maxage 是以天數為單位的。如果我們是以按天來輪轉日志,那么二者的差別就不大了。

4、nginx日志切割實例

vim /etc/logrotate.d/nginx   #創建nginx日志切割配置文件

/application/nginx/logs/*.log{
daily
rotate 10
create
dateext
}

logrotate -d /etc/logrotate.d/nginx    調試測試   -d debug
logrotate -d /etc/logrotate.d/nginx    手動切割日志測試

ls /application/nginx/logs/           帶日期的表示是切割好的日志
access.log           bbs.log-20180228   error.log           www.log
access.log-20180228  blog.log           error.log-20180228  www.log-20180228
bbs.log              blog.log-20180228  nginx.pid

配置好nginx切割日志生效時間

# cat /etc/anacrontab    #此文件里有生效時間
# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.
 
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45

# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22   #生效時間范圍是3點到22點

#period in days   delay in minutes   job-identifier   command
1       5       cron.daily              nice run-parts /etc/cron.daily
7       25      cron.weekly             nice run-parts /etc/cron.weekly
@monthly 45     cron.monthly            nice run-parts /etc/cron.monthly

也就是說,配好的nginx切割日志,生效時間是在凌晨3點到22點之間,而且隨機延遲時間是45分鍾

5、其他配置示例

/var/log/htmlaccess.log {
 errors jim
 notifempty
 nocompress
 weekly
 prerotate
 /usr/bin/chattr -a /var/log/htmlaccess.log
 endscript
 
 postrotate
 /usr/bin/chattr +a /var/log/htmlaccess.log
 endscript
}

持續集成系統日志處理配置

/var/log/jenkins/jenkins.log /var/log/jenkins/access_log {
    compress
    dateext
    maxage 365      #保留最大365天
    rotate 99       #最大保留99個備份
    size=+4096k
    notifempty
    missingok
    create 644
    copytruncate
}

 自定義日志處理

/medialog/*.log {
    create 0644 root root
    daily
    rotate 30
    missingok
    copytruncate
    notifempty
    compress
    delaycompress
    olddir /medialog/backlog    # 將歸檔日志單獨目錄存儲
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM