日誌就像程序的生命記錄儀,詳細記錄下了程序運行的點點滴滴。html
記錄哪些日誌須要你在編寫應用程序慎重決定,本文講述如何使用Linux自帶的logrotate程序來精心組織咱們可愛的日誌文件。mysql
日誌,實際就是本文文件,且是個內容不斷在增加的文件。處理一般就是按天或者按大小來備份、壓縮或轉儲,很是簡單。linux
/var/log/messages
是你們熟悉的系統日誌存放位置。若是該文件內容增加特別快,幾天可能就撐滿了整個硬盤,那麼對該文件的備份、壓縮或轉儲就顯得尤其重要。nginx
手動怎麼處理呢?多是:sql
Logrotate實際就是起着上述腳本做用的小工具,他經過讓用戶來配置規則的方式,檢測和處理日誌文件。配合Cron可以讓處理定時化;
Logrotate預製了大量判斷條件和處理方式,可大大下降手寫腳本的負擔和出錯的可能;tomcat
Logrorate檢測日誌文件屬性,比對用戶配置好的檢測條件,對知足條件的再根據用戶配置的要求來處理,整個能夠經過Cron來定時調度,這實際上是很是經典的Linux解決問題的思路,能夠好好靜下心來品味下,簡單,好用。ide
如下是logrotate運行的關鍵點:工具
/usr/bin/logrotate
程序所在位置;/etc/cron.daily/logrotate
默認讓Cron天天執行logrotate一次;/etc/logrotate.conf
全局配置文件;/etc/logrotate.d
應用自個的配置文件存放目錄,覆蓋全局配置;/etc/logrotate.conf
# cat /etc/logrotate.conf # see "man logrotate" for details # rotate log files weekly weekly # keep 4 weeks worth of backlogs rotate 4 # create new (empty) log files after rotating old ones create # use date as a suffix of the rotated file # dateext # uncomment this if you want your log files compressed #compress # RPM packages drop log rotation information into this directory include /etc/logrotate.d # no packages own wtmp and btmp -- we'll rotate them here /var/log/wtmp { monthly create 0664 root utmp minsize 1M rotate 1 } /var/log/btmp { missingok monthly create 0600 root utmp rotate 1 } # system-specific logs may be also be configured here.
查看全局配置文件cat /etc/logrotate.conf
,能夠發現#
是行註釋,配置項都是以關鍵字形式出現(那麼想知道每一個關鍵字的做用,最簡單的就是直接man查看了)。post
仔細看下每一個關鍵字的註釋,不難發現,好比:測試
/etc/logrotate.d
目錄,或者直接放在全局配置裏面。因而當logrotate
程序被執行時,按照字面意思logrotate
默認是想每週處理下日誌,對日誌最多輪轉保留4份,處理方式是不壓縮也不加時間戳,處理完後再生成個同名文件。固然這些是默認設置,還對wtmp和btmp日誌處理作了單獨要求而且include /etc/logrotate.d
目錄下還有一大堆處理要求。該目錄下配置文件,在logrotate
被執行後,都會不一個個讀取來執行。
/etc/logrotate.d/
# ls /etc/logrotate.d/ cups debug dracut httpd iptraf mysqld ppp psacct sssd subscription-manager syslog tomcat6 up2date vsftpd wpa_supplicant yum
例如查看下sssd
配置文件的內容:
# cat /etc/logrotate.d/sssd /var/log/sssd/*.log { weekly missingok notifempty sharedscripts rotate 2 compress postrotate /bin/kill -HUP `cat /var/run/sssd.pid 2>/dev/null` 2> /dev/null || true endscript }
能夠發現基本格式與全局配置文件/etc/logrotate.conf
一致,不難想象單獨爲某個日誌配置的要求優先級確定更高,若是與全局配置中出現相同項目的配置,單獨的確定覆蓋全局的。
/etc/cron.daily/logrotate
配置文件都指定完畢,logrotate可單獨執行了,固然也能夠經過cron來定時執行;
# cat /etc/cron.daily/logrotate #!/bin/sh /usr/sbin/logrotate /etc/logrotate.conf >/dev/null 2>&1 EXITVALUE=$? if [ $EXITVALUE != 0 ]; then /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]" fi exit 0
默認的logrotate已經放在/etc/cron.daily/logrotate
目錄,很明顯是讓cron天天執行一次logrotate程序;
固然你也能夠將該腳本放到其餘時間,好比每分鐘執行,甚至能夠單獨寫crontab表達式來讓logrotate指定配置文件和指定時間執行;
至此,咱們看過了logrotate的全局配置文件,單獨配置文件,已經如何配合cront來定時執行。
爲了加深記憶,咱們先小結下:
man過logrotate的同窗大概知道logrotate該怎麼用了,常見的選項使用以下:
# 1. 調試 (d = debug)參數爲配置文件,不指定則執行全局配置文件 logrotate -d /etc/logrotate.d/test.conf # 2. 強制執行(-f = force),能夠配合-v(-v =verbose)使用,注意調試信息默認攜帶-v; logrotate -v -f /etc/logrotate.d/test.conf
本例經過自定義配置文件來壓縮指定日誌文件來測試logrotate的使用。
注意logrotate都是須要使用root來執行的,(可是能夠經過配置項來指定生成的日誌文件爲普通用戶的)。
# 1. 生成一個日誌文件 man ps > test.log ll -h test.log -rw-r--r-- 1 root root 54K Sep 6 11:36 test.log # 2. 編寫對該日誌文件如何處理的logrotate配置文件 cat /etc/logrotate.d/test.conf /var/log/test.log { compress rotate 4 size 30k create 0600 root root } # 3. 調試是否能夠按照配置文件要求生成壓縮文件 logrotate -d /etc/logrotate.d/test.conf reading config file /etc/logrotate.d/test.conf reading config info for /var/log/test.log Handling 1 logs rotating pattern: /var/log/test.log 30720 bytes (4 rotations) empty log files are rotated, old logs are removed considering log /var/log/test.log log needs rotating rotating log /var/log/test.log, log->rotateCount is 4 dateext suffix '-20150906' glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' renaming /var/log/test.log.4.gz to /var/log/test.log.5.gz (rotatecount 4, logstart 1, i 4), renaming /var/log/test.log.3.gz to /var/log/test.log.4.gz (rotatecount 4, logstart 1, i 3), renaming /var/log/test.log.2.gz to /var/log/test.log.3.gz (rotatecount 4, logstart 1, i 2), renaming /var/log/test.log.1.gz to /var/log/test.log.2.gz (rotatecount 4, logstart 1, i 1), renaming /var/log/test.log.0.gz to /var/log/test.log.1.gz (rotatecount 4, logstart 1, i 0), renaming /var/log/test.log to /var/log/test.log.1 creating new /var/log/test.log mode = 0600 uid = 0 gid = 0 compressing log with: /bin/gzip removing old log /var/log/test.log.5.gz error: error opening /var/log/test.log.5.gz: No such file or directory # 4. 調試結果正常,實際測試下 logrotate -f /etc/logrotate.d/test.conf ll -h test.log* -rw------- 1 root root 0 Sep 6 11:44 test.log -rw-r--r-- 1 root root 14K Sep 6 11:44 test.log.1.gz
測試正常;
以上logrotate -f /etc/logrotate.d/test.conf
指令徹底能夠寫入crontab中,按照要求時間來執行,此處暫時不拆開講了。
咱們從上述debug信息中,摘錄輪轉部分的日誌來理解下,什麼叫輪轉。
rotating log /var/log/test.log, log->rotateCount is 4 renaming /var/log/test.log.4.gz to /var/log/test.log.5.gz (rotatecount 4, logstart 1, i 4), renaming /var/log/test.log.3.gz to /var/log/test.log.4.gz (rotatecount 4, logstart 1, i 3), renaming /var/log/test.log.2.gz to /var/log/test.log.3.gz (rotatecount 4, logstart 1, i 2), renaming /var/log/test.log.1.gz to /var/log/test.log.2.gz (rotatecount 4, logstart 1, i 1), renaming /var/log/test.log.0.gz to /var/log/test.log.1.gz (rotatecount 4, logstart 1, i 0), renaming /var/log/test.log to /var/log/test.log.1 emoving old log /var/log/test.log.5.gz error: error opening /var/log/test.log.5.gz: No such file or directory
根據配置文件要求,輪轉4份;
以僞代碼在簡寫上述日誌爲:
rotateCount=4 mv 4 5 mv 3 4 mv 2 3 mv 1 2 rm 5
這就很容易理解了,所謂輪轉,就是相似二級制向右位移同樣不斷的重命名;
在弄清楚logrotate的運做機制,又實地測試一番後,如下將經過多個例子方式來讓你們快速配置。
copytruncate的做用在於先複製一份當前日誌文件用作處理,再清空源日誌文件,讓其繼續接收日誌。
固然在複製和清空的空隙可能會有若干
$ cat logrotate.conf /tmp/output.log { size 1k copytruncate create 700 bala bala rotate 4 compress }
摘自參考3;
postrotate和endscript中間能夠編寫自定義腳本,用來對日誌或者其餘其定義處理,擴展性很是強;
例如因爲logrotate對壓縮日誌可指定的時間戳只能到天,因而能夠再自定義腳本里面對文件作時分等細化命名;
$ cat logrotate.conf /tmp/output.log { size 1k copytruncate rotate 4 compress postrotate /home/bala/myscript.sh endscript }
摘自參考3;
默認壓縮程序使用.gz
,固然能夠自定義,須要制定壓縮程序和後綴名;
$ cat logrotate.conf /tmp/output.log { size 1k copytruncate create compress compresscmd /bin/bzip2 compressext .bz2 rotate 4 }