Linux自帶的logrotate管理日志用法
利用Linux自帶的logrotate管理日志
日常運維中,經(jīng)常要對各類日志進行管理,清理,監(jiān)控,尤其是因為應用bug,在1小時內(nèi)就能寫幾十個G日志,導致磁盤爆滿,系統(tǒng)掛掉。
nohup.out,access.log,catalina.out
本文簡單介紹利用Linux自帶的logrotate來對操作系統(tǒng)中各類日志進行管理
1. logrotate簡介
為了使用它,主要有兩個地方需要修改一下:一個是/etc/logrotate.conf,另一個是/etc/logrotate.d/下面的文件。
你既可以在logrotate.conf中直接定義如何處理你的log文件,也可以在/logrotate.d/下面針對自己的log新建一個對應的文件來定義處理log的行為。
推薦在目錄 /logrotate.d/ 下面創(chuàng)建自己的文件來對個性化的日志進行處理。
logrotate定義了如何處理日志,而它本身則是被crond定時調(diào)用的。
我使用的一個生產(chǎn)實例:
/usr/local/nginx/logs/*.log {
create 0644 root root
daily
rotate 2
missingok
copytruncate
ifempty
compress
noolddir
}上述內(nèi)容保存到nginxlog文件,存放到目錄:/etc/logrotate.d/nginxlog
設置權限:
owner=root group=root mode=0644
測試配置是否正確:
lograte -d /etc/logrotate.d/nginxlog
執(zhí)行配置
lograte -f /etc/logrotate.d/nginxlog
2. logrotate配置參數(shù)
logrotate 全局配置文件: /etc/logrotate.conf
| 配置參數(shù) | 功能說明 |
|---|---|
| compress | 通過gzip 壓縮轉儲以后的日志 |
| nocompress | 不需要壓縮時,用這個參數(shù) |
| copytruncate | 用于還在打開中的日志文件,把當前日志備份并截斷;是先拷貝再清空的方式,拷貝和清空之間有一個時間差,可能會丟失部分日志數(shù)據(jù)。 |
| nocopytruncate | 備份日志文件但是不截斷 |
| create mode owner group | 轉儲文件,使用指定的文件模式創(chuàng)建新的日志文件。輪轉時指定創(chuàng)建新文件的屬性,如create 0777 nobody nobody |
| nocreate | 不建立新的日志文件 |
| delaycompress | 和 compress 一起使用時,轉儲的日志文件到下一次轉儲時才壓縮 |
| nodelaycompress | 覆蓋 delaycompress 選項,轉儲同時壓縮 |
| errors address | 專儲時的錯誤信息發(fā)送到指定的Email 地址 |
| ifempty | 即使是空文件也轉儲,這個是 logrotate 的缺省選項。 |
| notifempty | 如果是空文件的話,不轉儲 |
| mail address | 把轉儲的日志文件發(fā)送到指定的E-mail 地址 |
| nomail | 轉儲時不發(fā)送日志文件 |
| olddir directory | 轉儲后的日志文件放入指定的目錄,必須和當前日志文件在同一個文件系統(tǒng) |
| noolddir | 轉儲后的日志文件和當前日志文件放在同一個目錄下 |
| prerotate/endscript | 在logrotate轉儲之前需要執(zhí)行的指令,例如修改文件的屬性等動作;這兩個關鍵字必須單獨成行; |
| postrotate/endscript | 在logrotate轉儲之后需要執(zhí)行的指令,例如重新啟動 (kill -HUP) 某個服務!必須獨立成行; |
| daily | 指定轉儲周期為每天 |
| weekly | 指定轉儲周期為每周 |
| monthly | 指定轉儲周期為每月 |
| rotate count | 指定日志文件刪除之前轉儲的個數(shù),0 指沒有備份,5 指保留5個備份 |
| tabootext [+] list 讓logrotate | 不轉儲指定擴展名的文件,缺省的擴展名是:.rpm-orig, .rpmsave, v, 和 ~ |
| size | size當日志文件到達指定的大小時才轉儲,Size 可以指定 bytes (缺省)以及KB (sizek)或者MB (sizem). |
| missingok | 如果日志丟失,不報錯繼續(xù)滾動下一個日志 |
| notifempty | 當日志文件為空時,不進行輪轉 |
| sharedscripts | 運行postrotate腳本,作用是在所有日志都輪轉后統(tǒng)一執(zhí)行一次腳本。如果沒有配置這個,那么每個日志輪轉后都會執(zhí)行一次腳本 |
| dateext | 使用當期日期作為命名格式 |
| dateformat .%s | 配合dateext使用,緊跟在下一行出現(xiàn),定義文件切割后的文件名,必須配合dateext使用,只支持 %Y %m %d %s 這四個參數(shù) |
| size(或minsize) log-size | 當日志文件到達指定的大小時才轉儲,log-size能指定bytes(缺省)及KB (sizek)或MB(sizem). |
說明:
當日志文件 >= log-size 的時候就轉儲。
以下為合法格式:(其他格式的單位大小寫沒有試過)
- size = 5 或 size 5 (>= 5 個字節(jié)就轉儲)
- 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
postrotatemv /home/deploy/apps/production.log-* /data1/log/railsgzip /data1/log/rails/production.log-*
endscript
}問題:rotate和maxage的區(qū)別是什么?
它們都是用來控制保存多少日志文件的,區(qū)別在于 rotate 是以個數(shù)為單位的,而 maxage 是以天數(shù)為單位的。如果我們是以按天來輪轉日志,那么二者的差別就不大了。
3. nginx日志切割實例
vim /etc/logrotate.d/nginx #創(chuàng)建nginx日志切割配置文件
/application/nginx/logs/*.log{
daily
rotate 10
create
dateext
}
logrotate -d /etc/logrotate.d/nginx 調(diào)試測試 -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 command1 5 cron.daily nice run-parts /etc/cron.daily7 25 cron.weekly nice run-parts /etc/cron.weekly @monthly 45 cron.monthly nice run-parts /etc/cron.monthly
也就是說,配好的nginx切割日志,生效時間是在凌晨3點到22點之間,而且隨機延遲時間是45分鐘
4. 其他配置示例
/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
}持續(xù)集成系統(tǒng)日志處理配置
/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 # 將歸檔日志單獨目錄存儲
}總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
網(wǎng)站加速VPS篇 memcache和memcached安裝方法
Memcache是一個自由和開放源代碼、高性能、分配的內(nèi)存對象緩存系統(tǒng)。用于加速動態(tài)web應用程序,減輕數(shù)據(jù)庫負載。2010-12-12
Linux Apache+Proftpd構建虛擬主機時要注意的幾個安全問題
Linux下Apache+Proftpd構建虛擬主機時要注意的幾個安全問題,大家可以參考下,有其它未完整的地方,大家可以補充下。2009-08-08
Linux學習第三篇 Centos7安裝mysql5.7.16數(shù)據(jù)庫
這篇文章主要為大家詳細介紹了Linux學習第三篇,Centos7安裝mysql5.7.16數(shù)據(jù)庫,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05

