logrotate实现日志文件轮转

介绍

logrotate是基于linux系统的日志管理工具,可用于切割、删除、转储日志文件,logrotate是基于crontab运行的,其配置文件为/etc/logrotate.conf,也可以将自定义的配置文件放在/etc/logrotate.d目录下,可覆盖logrotate.conf的配置。

安装

通常Linux默认安装了logrotate,可以通过以下命令检查是否已经安装:

root@server:~# logrotate --version
logrotate 3.19.0

    Default mail command:       /usr/bin/mail
    Default compress command:   /bin/gzip
    Default uncompress command: /bin/gunzip
    Default compress extension: .gz
    Default state file path:    /var/lib/logrotate/status
    ACL support:                yes
    SELinux support:            yes

安装后其执行文件为/usr/sbin/logrotate,其并不是系统服务和守护进程。

配置

默认配置

/etc/logrotate.conf为默认的配置文件,其中的配置向对于所有的日志文件都生效,如果要对特定的日志文件进行配置,需要在/etc/logrotate.d/目录下单独添加配置文件。其默认配置如下:

# see "man logrotate" for details

# global options do not affect preceding include directives

# rotate log files weekly
weekly

# use the adm group by default, since this is the owning group
# of /var/log/syslog.
su root adm

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
#dateext

# uncomment this if you want your log files compressed
#compress

# packages drop log rotation information into this directory
include /etc/logrotate.d

# system-specific logs may also be configured here.

logrotate参数

参数说明
daily/weekly/monthly/yearly日志轮换周期,每天、每周、每月、每年
size日志文件达到指定大小时轮换,默认单位bytes,如:size 50M, size 1G。
rotate日志文件的保留个数,默认为0,如:rotate 5。
maxage指定日志文件的最大存在时间。
minsize指定日志文件的最小大小。
maxsize指定日志文件的最大大小。
olddir
noolddir
将转出的日志文件存放到指令目录,如olddir ./xxxx/,默认为当前目录下。
compress
nocompress
压缩旧日志文件。
delaycompress
nodelaycomporess
在下一次轮换时才压缩日志文件。
missingok如果日志文件不存在,则忽略错误。
notifempty如果日志文件为空,则不轮换。
copytruncate使用复制和截断来实现日志文件的轮换。
dateext在轮换后的日志文件名中添加日期扩展,默认为-%Y%m%d,可用dataformat修改。
dateformat自定义日期格式,用于日志文件名的日期扩展,配合dateext使用。
extension指定压缩的日志文件扩展名。
ifempty如果日志文件为空,则仍然进行轮换。
create创建新的日志文件并设置权限、所有者和组,如:create 744 root root。
su指定以特定用户身份执行轮换操作。
sharedscripts在每个日志文件轮换之后执行一次postrotate脚本。
prerotate
endscript
在轮换之前执行特定的命令。
postrotate
endscript
在轮换之后执行特定的命令。
firstaction
endscript
在第一次轮换之前执行特定的命令。
lastaction
endscript
在最后一次轮换之后执行特定的命令。

运行

debug模式

用于测试配置文件的正确性,仅输出debug信息,不执行操作。

logrotate -d <配置文件>

verbose模式

会根据配置执行操作,并打印详细信息。

logrotate -v <配置文件>

force模式

测试时如果日期不达到配置中的轮换时间,执行logrotate命令时也不会进行转储,可以通过-f进行强制执行。

logrotate -f <配置文件>

定时执行

cron.daily

logrotate是基于cron运行的,执行时间通过cron的配置进行设定,其配置文件为/etc/crontab/etc/anacrontab

/etc/cron.daily/目录下包含logrotate脚本,每日执行logrotate命令,如果自行配置了crontab,可能导致转储操作执行两次。

cron.daily的执行时间配置在/etc/crontab中。

25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )

以上配置表示cron.daily在每天的6点25分执行。这样日志转储的时间也是6点25分。如果想不按照这个时间进行日志转储,则logrotate配置文件不能放在/etc/logrotate.d/目录下,否则将会执行两次。

自定义crontab

  1. 编辑 crontab 文件:
crontab -e
  1. 添加定时配置:
* * * * * logrotate /root/logrotate/test

该配置将每分钟执行一次转储操作。

配置示例

按文件大小转储

/var/log/test/*.log {
	size 30
	rotate 5
	copytruncate
	missingok
	compress
	delaycompress
	create 0640 root root
}

对于原始的test.log文件,将被转储为文件名为test.log-20231223的文件,且已转储的文件将被压缩成test.log-20231222.gz文件。

按日期转储

/var/log/test/*.log {
	daily
	rotate 5
	copytruncate
	dateext
	missingok
	compress
	delaycompress
	create 0640 root root
}

按每日对日志文件进行转储时,logrotate配置文件可以放在/etc/logrotate.d/目录下,通过默认配置的cron.daily进行定时触发。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值