logrotate,日志文件收割机
2021-10-25 本文已影响0人
halfempty
1 问题
日志很重要, 通过它可以知晓过去一段时间程序经历了什么
我们期望日志记录地更多更全, 于是出现了新的问题
- 日志量庞大, 存储吃紧
- 单日志文件过大, 不利于检索
因此需要做出取舍, 通常采取折中的方案, 记录日志的同时, 限制日志的大小以及保留期限
2 案例
去/var/log
目录下, 能发现大量带日期的日志文件, 如
- cron
- maillog
- messages
- secure
- vsftpd
每类日志包含5个文件, 且日期后缀相差7
如何实现这种滚动效果, 以及如何将效果应用到自定义日志文件中?
比如
- 通过nohup启动的程序会生成nohup.out文件
- tomcat的catalina.out文件
3 原理
centos7.5最小化安装自带logrotate
, 望文生义
3.1 what
man logrotate
手册解释如下
logrotate is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files.
3.2 when
logrotate
通过cron
调度, 执行周期为每天
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
3.3 how
如何知道哪些日志文件需要滚动, 以及如何滚动?
这时就需要知道logrotate
的配置文件
- /etc/logrotate.conf
- /etc/logrotate.d/*
全局配置项
- weekly, 滚动周期为一周
- rotate 4, 保留4份滚动日志
- create, 触发滚动条件后, 重新创建新文件
- dateext, 滚动日志文件添加日期(YYYYMMDD)后缀
- compress, 压缩滚动日志
详细参数可以参见man logrotate
此时看下系统日志的相关配置, 便能理解日志文件的生成规律了
cat /etc/logrotate.d/syslog
/var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler
{
missingok
sharedscripts
postrotate
/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
endscript
}
那么, 如果想解决nohup.out文件过大, 你知道该如何配置了吗?