Linux使用logrotate來(lái)切割日志文件
程序在運(yùn)行的時(shí)候?yàn)榱肆私膺\(yùn)行狀態(tài),會(huì)輸出日志文件,時(shí)間久了日志文件會(huì)變得非常大,甚至達(dá)到GB級(jí)別。我在golang應(yīng)用里使用logrus包來(lái)打日志,配置和使用都很方便,就是沒(méi)有日志分割的功能,應(yīng)用在線上運(yùn)行一個(gè)月后日志文件都已經(jīng)達(dá)到上百兆。后來(lái)發(fā)現(xiàn)了logrotate,這是centos自帶的日志分割工具,都不用安裝額外組件就能實(shí)現(xiàn)定時(shí)分割日志。
1.運(yùn)行原理
logrotate由系統(tǒng)的cron運(yùn)行,位置在/etc/cron.daily/logrotate
#!/bin/sh /usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf EXITVALUE=$? if [ $EXITVALUE != 0 ]; then /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]" fi exit 0
可以看到入口配置文件是/etc/logrotate.conf,依次運(yùn)行/etc/logrotate.conf.d里的配置文件 如果發(fā)現(xiàn)配置的logrotate沒(méi)有執(zhí)行,可以看下系統(tǒng)的crond服務(wù)有沒(méi)有開(kāi)啟
2.配置
如果有安裝nginx,可以參考nginx里的配置例子
/var/log/nginx/*log { create 0644 nginx nginx daily rotate 10 missingok notifempty compress sharedscripts postrotate /bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 2>/dev/null || true endscript }
第一行定義的是日志文件的路徑,可以用*通配,一般可以定義成*.log來(lái)匹配所有日志文件。也可以指定多個(gè)文件,用空格隔開(kāi),比如
/var/log/nginx/access.log /var/log/nginx/error.log { }
花括號(hào)里面是日志切割相關(guān)的參數(shù),下面是常用的切割參數(shù)
- compress 是否開(kāi)啟壓縮,壓縮格式gzip
- 不開(kāi)啟壓縮
- compresscmd 自定義壓縮命令
- compressexty 壓縮文件名后綴
- compressoptions 壓縮選項(xiàng)
- copy 復(fù)制一份文件
- create 后面跟mode owner group,設(shè)置新日志文件的權(quán)限
- daily 按天分割
- weekly 按周分割
- monthly 按月分割
- rotate 后面跟數(shù)字,表示需要保留的文件歷史記錄,超過(guò)數(shù)量就會(huì)刪除,或者通過(guò)郵件發(fā)送
- size 后面跟文件大小,比如100k、100M,超過(guò)這個(gè)大小后分割
- missingok 忽略不存在的文件,不報(bào)錯(cuò)
- notifempty 不分割空文件
- sharedscripts 配合postrotate、prerotate,讓他們只執(zhí)行一次
- postrotate/endscript 文件分割完后,執(zhí)行postrotate、endscript之間的命令
- prerotate/endscript 文件分割完前,執(zhí)行prerotate、endscript之間的命令
下面看幾個(gè)例子
/var/log/httpd/error.log { rotate 5 mail i@wuyuans.com size=100k sharedscripts postrotate /sbin/killall -HUP httpd endscript }
切割/var/log/httpd/error.log日志文件,超過(guò)100k后切割,保留最新的5個(gè)歷史記錄,超過(guò)5個(gè)的郵件發(fā)送到fss@qq.com,postrotate里的的命令是為了讓httpd重新打開(kāi)日志文件。
/var/lib/mysql/mysqld.log { # create 600 mysql mysql notifempty daily rotate 3 missingok compress postrotate # just if mysqld is really running if test -x /usr/bin/mysqladmin && \ /usr/bin/mysqladmin ping &>/dev/null then /usr/bin/mysqladmin --local flush-error-log \ flush-engine-log flush-general-log flush-slow-log fi endscript }
這是對(duì)mysql日志的切割,每天一份,忽略空文件,保留最新3份,使用gzip壓縮
/home/wuyuan/log/*.log { su wuyuan wuyuan create 0777 wuyuan wuyuan daily rotate 10 olddir /home/wuyuan/log/old missingok postrotate endscript nocompress }
這是我在用的配置項(xiàng),對(duì)log目錄所有.log文件切割,每天一份,保留10份,新文件設(shè)定權(quán)限777,歷史文件保留在old目錄里,這樣可以方便查看。因?yàn)閼?yīng)用程序用的logrus使用append的方式寫日志,所以不需要重新打開(kāi)日志文件,這點(diǎn)logrus做得很不錯(cuò)。
3.測(cè)試
寫完配置文件后可以手動(dòng)執(zhí)行下,來(lái)驗(yàn)證是否可用。
logrotate -f /etc/logrotate.d/wuyuan
其中-f 表示強(qiáng)制執(zhí)行,其他命令可以用help來(lái)查看
logrotate --help 用法: logrotate [OPTION...] <configfile> -d, --debug Don't do anything, just test (implies -v) -f, --force Force file rotation -m, --mail=command Command to send mail (instead of `/bin/mail') -s, --state=statefile Path of state file -v, --verbose Display messages during rotation -l, --log=STRING Log file --version Display version information Help options: -?, --help Show this help message --usage Display brief usage message
沒(méi)問(wèn)題的話日志就會(huì)被移到old目錄下,并帶上日期,之前的log文件會(huì)被清空
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Linux簡(jiǎn)介及最常用命令(簡(jiǎn)單易學(xué),但能解決95%以上的問(wèn)題)
這篇文章主要介紹了Linux簡(jiǎn)介及最常用命令(簡(jiǎn)單易學(xué),但能解決95%以上的問(wèn)題),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-08-08linux網(wǎng)絡(luò)參數(shù)配置方法詳解
這篇文章主要介紹了linux網(wǎng)絡(luò)參數(shù)的配置方法,這樣可以讓你的服務(wù)器訪問(wèn)網(wǎng)絡(luò),主要參數(shù):IP地址、子網(wǎng)掩碼、網(wǎng)關(guān)、DNS2013-11-11Linux命令學(xué)習(xí)總結(jié):詳解reboot命令
這篇文章主要介紹了Linux命令學(xué)習(xí)總結(jié):詳解reboot命令,這個(gè)指令使用起來(lái)非常簡(jiǎn)單,有興趣的可以了解一下。2016-11-11