Linux日志管理神器之Logrotate日志分割

✍️Auth:star皆空       Date:2021/11/4       Cat:Linux服务器       👁️:1,072 次浏览

前言:

有些服务,会自动产生大量的日志文件,如果不限制,会占用磁盘空间。
如果单纯的用定时任务crontab删除,又不太灵活,这时需要日志神器logrotate。

logrotate工具是系统自带为了方便进行日志管理而产生的一个工具。
系统会定时运行 logrotate,一般是每天一次。也是基于定时任务crontab运行的。

配置文件:

主配置文件位置在 /etc/logrotate.conf,一般配置在 /etc/logrotate.d/子目录下。
如系统默认日志配置:

$ vim /etc/logrotate.d/log_file 

/var/log/log_file {

    monthly
    rotate 5
    compress
    delaycompress
    missingok
    notifempty
    create 644 root root
    postrotate
        /usr/bin/killall -HUP rsyslogd
    endscript
}

配置文件参数:

  • daily :指定转储周期为每天
  • weekly :指定转储周期为每周
  • monthly :指定转储周期为每月
  • rotate count :指定日志文件删除之前转储的次数,0 指没有备份,5 指保留 5 个备份
  • tabooext [+] list:让 logrotate 不转储指定扩展名的文件,缺省的扩展名是:.rpm-orig, .rpmsave, v, 和~
  • missingok:在日志轮循期间,任何错误将被忽略,例如 “文件无法找到” 之类的错误。
  • size size:当日志文件到达指定的大小时才转储,bytes (缺省) 及 KB (sizek) 或 MB (sizem)
  • compress: 通过 gzip 压缩转储以后的日志
  • nocompress: 不压缩
  • copytruncate:用于还在打开中的日志文件,把当前日志备份并截断
  • nocopytruncate: 备份日志文件但是不截断
  • create mode owner group : 转储文件,使用指定的文件模式创建新的日志文件
  • nocreate: 不建立新的日志文件
  • delaycompress: 和 compress 一起使用时,转储的日志文件到下一次转储时才压缩
  • nodelaycompress: 覆盖 delaycompress 选项,转储同时压缩。
  • errors address : 专储时的错误信息发送到指定的 Email 地址
  • ifempty :即使是空文件也转储,这个是 logrotate 的缺省选项。
  • notifempty :如果是空文件的话,不转储
  • mail address : 把转储的日志文件发送到指定的 E-mail 地址
  • nomail : 转储时不发送日志文件
  • olddir directory:储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统
  • noolddir: 转储后的日志文件和当前日志文件放在同一个目录下
  • prerotate/endscript: 在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行

更多信息请参考man logrotate帮助文档

模板是通用的,而配置参数则根据你的需求进行调整,不是所有的参数都是必要的。

/var/log/log_file {
    size=50M
    rotate 5
    dateext
    create 644 root root
    postrotate
        /usr/bin/killall -HUP rsyslogd
    endscript
}

在上面的配置文件中,我们只想要轮询一个日志文件,size=50M 指定日志文件大小可以增长到 50MB,dateext 指 示让旧日志文件以创建日期命名。

可自行参考/etc/logrotate.d/目录下系统默认的文件。

logrotate命令:

具体 logrotate 命令格式如下:

logrotate [OPTION...] <configfile>
-d, --debug :debug 模式,测试配置文件是否有错误。
-f, --force :强制转储文件。
-m, --mail=command :压缩日志后,发送日志到指定邮箱。
-s, --state=statefile :使用指定的状态文件。
-v, --verbose :显示转储过程。

要为某个特定的配置调用 logrotate:

 logrotate /etc/logrotate.d/log_file

排障过程中的最佳选择是使用-d选项以预演方式运行 logrotate。要进行验证,不用实际轮循任何日志文件, 可以模拟演练日志轮循并显示其输出。

[root@localhost ~]$ logrotate -d /etc/logrotate.d/log_file

WARNING: logrotate in debug mode does nothing except printing debug messages!  Consider using verbose mode (-v) instead if this is not what you want.

reading config file /etc/logrotate.d/dnmplog
Reading state from file: /var/lib/logrotate/status
Allocating hash table for state file, size 64 entries
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state

Handling 1 logs

rotating pattern: /home/www/localhost/storage/logs  104857600 bytes (6 rotations)
empty log files are not rotated, old logs are removed
No logs found. Rotation not needed.

正如我们从上面的输出结果可以看到的,logrotate 判断该轮循是不必要的。如果文件的时间小于一天,这就会发生了。

强制轮循即使轮循条件没有满足,我们也可以通过使用-f选项来强制 logrotate 轮循日志文件,-v参数提供了详细的输出。

logrotate -vf /etc/logrotate.d/log_file 

个人项目配置:

项目每天可产生20G左右的日志,显示不能做每日轮询,所以额外需要添加定时任务做每小时,或者每隔多少分钟。

/home/www/localhost/storage/logs/log_*.log {

    su root root
    size 100M
    rotate 5
    compress
    delaycompress
    missingok
    notifempty
    nocreate 
}

如果轮询日志异常报错如下:
error: skipping “” because parent directory has insecure permissions (It’s world writable or writable by group which is not “root”) Set “su” directive in config file to tell logrotate which user/group should be used for rotation.”

需要加 su root root 选项。

同时添加定时任务:

crontab -e
*/40 * * * * sudo usr/sbin/logrotate -f /etc/logrotate.d/dnmplog
0 4 * * * sudo find /home/dnmp/www/localhost/storage/logs -mtime +1 -name "log_*.log*" -exec rm -fr {} \;

定时任务说明:
第一条,每隔40分钟轮询执行一次logrotate任务。

第二条,每天凌晨4点删除前一天的日志,原因如下:
由于项目系统产生日志格式原因,会导致以下情况:
如果昨天的分割到4个后(或者1,2,3),时间到第二天后,没法转储递增,会一直停留在4,没法删除,每天会有,久而久之,也会占用磁盘空间。所以需要定时删除。

log_20211104.log.1
log_20211104.log.2
log_20211104.log.3
log_20211104.log.4
log_20211105.log.1
log_20211105.log.2
log_20211105.log.3
log_20211105.log.4
log_20211105.log.5

设置完成

开启定时任务日志,以便查看定时任务是否执行。后续可取消。

sudo vim /etc/rsyslog.d/50-default.conf

cron.* /var/log/cron.log #将cron前面的注释符去掉

重启rsyslog

sudo systemctl  restart rsyslog
#查看定时任务执行日志
cat /var/log/cron.log
打赏作者

发表评论