一、Logrotate简介
1、Logrotate实际就是对日志进行切割的小工具,他通过让用户来配置规则的方式,检测和处理日志文件。配合Cron可让处理定时化;
2、Logrotate预制了大量判断条件和处理方式,可大大降低手写脚本的负担和出错的可能;
3、Logrorate检测日志文件属性,比对用户配置好的检测条件,对满足条件的再根据用户配置的要求来处理,整个可以通过Cron来定时调度,这其实是非常经典的Linux解决问题的思路,可以好好静下心来品味下,简单,好用。
二、Logrotate运行机制
系统会定时运行logrotate,一般是每天一次。系统是这么实现按天执行的。crontab会每天定时执行/etc/cron.daily目录下的脚本,而这个目录下有个文件叫logrotate
[root@svn ~]# cat /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
三、Logrotate组成
以下是logrotate运行的关键点:
/usr/bin/logrotate #程序所在位置; /etc/cron.daily/logrotate #默认让Cron每天执行logrotate一次; /etc/logrotate.conf #全局配置文件; /etc/logrotate.d #应用自个的配置文件存放目录,覆盖全局配置;
四、安装配置Logrotate
yum install -y logrotate
配置文件logrotate
[root@svn ~]# ll /usr/local/tomcat/logs/catalina.out -rw-r----- 1 root root 954 10月 15 2018 /usr/local/tomcat/logs/catalina.out [root@svn ~]# cat /etc/logrotate.d/tomcat.conf /usr/local/tomcat/logs/catalina.out { daily copytruncate rotate 30 compress notifempty dateext missingok }
配置文件参数说明:
daily #表示每天整理一次 rotate 20 #表示保留20天的备份文件 dateext #文件后缀是日期格式,也就是切割后文件是:xxx.log-20171205.gz copytruncate #用于还在打开中的日志文件,把当前日志备份并截断 compress #通过gzip压缩转储以后的日志(gzip -d xxx.gz解压) missingok #如果日志不存在则忽略该警告信息 notifempty #如果是空文件的话,不转储 #size 5M #当catalina.out大于5M就进行切割,可用可不用!
五、对日志进行切割测试
1、调试 (d = debug)参数为配置文件,不指定则执行全局配置文件
[root@svn logs]# logrotate -d /etc/logrotate.d/tomcat.conf reading config file /etc/logrotate.d/tomcat.conf Allocating hash table for state file, size 15360 B Handling 1 logs rotating pattern: /usr/local/tomcat/logs/catalina.out after 1 days (30 rotations) empty log files are not rotated, old logs are removed considering log /usr/local/tomcat/logs/catalina.out error: log /usr/local/tomcat/logs/catalina.out last rotated in the future -- rotation forced log needs rotating rotating log /usr/local/tomcat/logs/catalina.out, log->rotateCount is 30 dateext suffix '-20181009' glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' destination /usr/local/tomcat/logs/catalina.out-20181009.gz already exists, skipping rotation
2、强制执行(-f = force),可以配合-v(-v =verbose)使用,注意调试信息默认携带-v
[root@svn logs]# logrotate -v -f /etc/logrotate.d/tomcat.conf reading config file /etc/logrotate.d/tomcat.conf Allocating hash table for state file, size 15360 B Handling 1 logs rotating pattern: /usr/local/tomcat/logs/catalina.out forced from command line (30 rotations) empty log files are not rotated, old logs are removed considering log /usr/local/tomcat/logs/catalina.out log needs rotating rotating log /usr/local/tomcat/logs/catalina.out, log->rotateCount is 30 dateext suffix '-20181009' glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' copying /usr/local/tomcat/logs/catalina.out to /usr/local/tomcat/logs/catalina.out-20181009 truncating /usr/local/tomcat/logs/catalina.out compressing log with: /bin/gzip
效果如下:
[root@svn logs]# ll /usr/local/tomcat/logs/ 总用量 128 -rw-r----- 1 root root 87776 10月 9 11:23 catalina.2018-10-09.log -rw-r----- 1 root root 7127 10月 15 2018 catalina.2018-10-15.log -rw-r----- 1 root root 0 10月 9 13:45 catalina.out -rw-r----- 1 root root 328 10月 15 2018 catalina.out-20181009.gz -rw-r----- 1 root root 0 10月 9 10:21 host-manager.2018-10-09.log -rw-r----- 1 root root 0 10月 15 2018 host-manager.2018-10-15.log -rw-r----- 1 root root 6290 10月 9 10:46 localhost.2018-10-09.log -rw-r----- 1 root root 0 10月 15 2018 localhost.2018-10-15.log -rw-r----- 1 root root 16583 10月 9 11:02 localhost_access_log.2018-10-09.txt -rw-r----- 1 root root 0 10月 15 2018 localhost_access_log.2018-10-15.txt -rw-r----- 1 root root 0 10月 9 10:21 manager.2018-10-09.log -rw-r----- 1 root root 0 10月 15 2018 manager.2018-10-15.log
这个工具会自动每天帮我们分隔日志并进行打包,不需要我们在写crontab的脚本,非常的好用!