1. 安装

Linux一般是默认自带logrotate的,如果没有可以使用yum/apt安装

apt/yum 安装

1
2
3
4
5
6
[root@VM-145-82-centos ~]# yum install logrotate
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Package logrotate-3.8.6-14.tl2.x86_64 already installed and latest version
Nothing to do
[root@VM-145-82-centos ~]#
1
2
3
4
5
6
root@VM-0-15-ubuntu:[10:59:47]:~# apt-get install logrotate
Reading package lists... Done
Building dependency tree
Reading state information... Done
logrotate is already the newest version (3.8.7-2ubuntu2.16.04.2).
0 upgraded, 0 newly installed, 0 to remove and 385 not upgraded.

源码安装

github地址: https://github.com/logrotate/logrotate

按照github安装

2. 配置

logrotate是利用系统crontab定时执行的,在目录/etc/cron.daily中有个logrotate的脚本。如果需要,可以在cron.hourly,也可以在/etc/crontab中增加自己的配置。

这些配置都是独立的,结合自己的配置以及服务的日志量来自定义达到最优配置。

1
2
3
4
5
6
7
8
#!/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

配置文件主要存放在目录

1
2
[root@VM-145-82-centos ~]# ls /etc/logrotate.d/
conman cron.30m iptraf-ng mgetty mongodb mysql psacct syslog yum

一般安装会添加常用组件的配置,在生产环境中,我们会自定义一些配置。

如下是本人常用的配置:

1
2
3
4
5
6
7
8
9
10
11
/data/log/*.log
{
daily
notifempty
copytruncate
compress
rotate 60
missingok
dateext
dateformat -%s
}
配置参数 说明
monthly 日志文件将按月轮循。其它可用值为’daily’,’weekly’或者’yearly’。
rotate 5 一次将存储5个归档日志。对于第六个归档,时间最久的归档将被删除。
compress 在轮循任务完成后,已轮循的归档将使用gzip进行压缩。
delaycompress 总是与compress选项一起用,delaycompress选项指示logrotate不要将最近的归档压缩,压缩将在下一次轮循周期进行。这在你或任何软件仍然需要读取最新归档时很有用。
missingok 在日志轮循期间,任何错误将被忽略,例如“文件无法找到”之类的错误。
notifempty 如果日志文件为空,轮循不会进行。
create 644 root root 以指定的权限创建全新的日志文件,同时logrotate也会重命名原始日志文件。
size 日志文件大小的配置,如果没达到这个大小,将不会压缩
dateext 压缩文件带上日期,默认会使用编号(log.log.1.gz),该选项会是每次压缩都带上日期,如 log.log-2020
dateformat 日期格式 支持 %Y%m%d(年月日) %s(时间戳)
postrotate/endscript 在所有其它指令完成后,postrotate和endscript里面指定的命令将被执行。在这种情况下,rsyslogd 进程将立即再次读取其配置并继续运行。

3. 执行频度定制

logrotate是在cron中执行的,因此要自定义执行频度,可以增加crontab配置

比如,我们的服务需要每半小时压缩一次 hourly是不满足我们的需求,就需要在crontab中增加一条记录

1
2
3
4
[root@VM-145-82-centos /etc/logrotate.d/cron.30m]# crontab -e

# rotate nginx log every 30min
*/30 * * * * /usr/sbin/logrotate /etc/logrotate.d/cron.30m/* -f

可以将30min执行的所有配置文件放到一个目录,执行30min命令。

4. 手动执行

配置文件是否配置成功,执行后是什么效果。logrotate提供测试的功能。

1
2
3
4
5
6
7
8
9
10
11
[root@VM-145-82-centos /etc/logrotate.d/cron.30m]# logrotate -d /etc/logrotate.d/cron.30m/*
reading config file /etc/logrotate.d/cron.30m/nginx
Allocating hash table for state file, size 15360 B

Handling 1 logs

rotating pattern: /var/log/nginx/*log after 1 days (10 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/nginx/access.log
log does not need rotating (log is empty)considering log /var/log/nginx/error.log
log does not need rotating (log is empty)not running postrotate script, since no logs were rotated

执行后,会输出文件怎么变更,压缩重命名等。比如上面会提示 “old logs are removed” “10 rotate” 等记录。

总结

logrotate工具对于防止因庞大的日志文件而耗尽存储空间是十分有用的。配置完毕后,进程是全自动的,可以长时间在不需要人为干预下运行。可以根据需求及日志量定制自己的日志切割规则。