使用 Linux logrotate 命令轮转和归档日志
使用此 Linux 命令保持日志文件最新。下载新的 logrotate 备忘单。
<元字符集=“utf-8”>
日志非常适合查找应用程序正在执行的操作或排除可能出现的问题。几乎我们处理的每个应用程序都会生成日志,并且我们希望我们自己开发的应用程序也能生成日志。日志越详细,我们拥有的信息就越多。但如果放任不管,原木可能会增长到难以管理的大小,反过来,它们本身也会成为一个问题。因此,最好对它们进行修剪,保留我们需要的,并将其余的存档。
基本
logrotate
实用程序非常适合管理日志。它可以旋转它们、压缩它们、通过电子邮件发送它们、删除它们、存档它们,并在需要时启动新的文件。
运行 logrotate 非常简单 - 只需运行 logrotate -vs state-file config-file 即可。在上面的命令中,v
选项启用详细模式,s
指定状态文件,最后的config-file
提到配置文件,您可以在其中指定需要完成的操作。
动手实践
让我们检查一下在我们的系统上静默运行的 logrotate
配置,它管理我们在 /var/log
目录中找到的大量日志。查看该目录中的当前文件。您是否看到很多 *.[number].gz
文件?这就是 logrotate
正在做的事情。您可以在 /etc/logrotate.d/rsyslog
下找到此配置文件。我的看起来像这样:
/var/log/syslog
{
rotate 7
daily
missingok
notifempty
delaycompress
compress
postrotate
reload rsyslog >/dev/null 2>&1 || true
endscript
}
/var/log/mail.info
/var/log/mail.warn
/var/log/mail.err
/var/log/mail.log
/var/log/daemon.log
/var/log/kern.log
/var/log/auth.log
/var/log/user.log
/var/log/lpr.log
/var/log/cron.log
/var/log/debug
/var/log/messages
{
rotate 4
weekly
missingok
notifempty
compress
delaycompress
sharedscripts
postrotate
reload rsyslog >/dev/null 2>&1 || true
endscript
}
该文件首先定义旋转 /var/log/syslog
文件的指令,这些指令包含在后面的大括号内。他们的意思是这样的:
rotate 7
:保留最近七次旋转的日志。然后开始删除它们。daily
:每天轮换日志。与rotate 7
一起使用,这意味着将保留过去 7 天的日志。其他选项包括每周
、每月
、每年
。 还有一个size
参数,如果日志文件的大小超过指定限制,该参数将轮换日志文件 - 例如,size 10k
,大小 10M
、大小 10G
等。如果未指定任何内容,则每当logrotate
运行时都会轮换日志。您甚至可以在cron
中运行logrotate
,以在更具体的时间间隔使用它。missingok
:日志文件丢失也没关系。不要恐慌。notifempty
:如果日志文件为空,则不轮换。delaycompress
:如果压缩已打开,则延迟压缩直到下一次旋转。这允许至少存在一个旋转但未压缩的文件。如果您希望昨天的日志保持未压缩状态以进行故障排除,则非常有用。 如果某些程序在重新启动/重新加载之前仍可能写入旧文件(例如 Apache),这也会很有帮助。compress
:压缩已开启。使用nocompress
将其关闭。postrotate/endscript
:旋转后运行此部分中的脚本。 有助于进行清理工作。还有一个prerotate/endscript
用于在轮换开始之前做一些事情。
您能弄清楚下一部分对上面配置中提到的所有文件有何作用吗? 第二部分中唯一的附加参数是 sharedscripts
,它告诉 logrotate
在所有日志轮换完成之前不要运行 postrotate/endscript
中的部分。它可以防止为每个轮转的日志执行脚本并在最后运行一次。
新鲜玩意
我使用以下配置来处理系统上的 Nginx 访问和错误日志。
/var/log/nginx/access.log
/var/log/nginx/error.log {
size 1
missingok
notifempty
create 544 www-data adm
rotate 30
compress
delaycompress
dateext
dateformat -%Y-%m-%d-%s
sharedscripts
extension .log
postrotate
service nginx reload
endscript
}
可以使用以下命令运行上述脚本:
logrotate -vs state-file /tmp/logrotate
第一次运行该命令会给出以下输出:
reading config file /tmp/logrotate
extension is now .log
Handling 1 logs
rotating pattern: /var/log/nginx/access.log
/var/log/nginx/error.log 1 bytes (30 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/nginx/access.log
log needs rotating
considering log /var/log/nginx/error.log
log does not need rotating
rotating log /var/log/nginx/access.log, log->rotateCount is 30
Converted ' -%Y-%m-%d-%s' -> '-%Y-%m-%d-%s'
dateext suffix '-2021-08-27-1485508250'
glob pattern '-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding logs to compress failed
glob finding old rotated logs failed
renaming /var/log/nginx/access.log to /var/log/nginx/access-2021-08-27-1485508250.log
creating new /var/log/nginx/access.log mode = 0544 uid = 33 gid = 4
running postrotate script
* Reloading nginx configuration nginx
并第二次运行它:
reading config file /tmp/logrotate
extension is now .log
Handling 1 logs
rotating pattern: /var/log/nginx/access.log
/var/log/nginx/error.log 1 bytes (30 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/nginx/access.log
log needs rotating
considering log /var/log/nginx/error.log
log does not need rotating
rotating log /var/log/nginx/access.log, log->rotateCount is 30
Converted ' -%Y-%m-%d-%s' -> '-%Y-%m-%d-%s'
dateext suffix '-2021-08-27-1485508280'
glob pattern '-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
compressing log with: /bin/gzip
renaming /var/log/nginx/access.log to /var/log/nginx/access-2021-08-27-1485508280.log
creating new /var/log/nginx/access.log mode = 0544 uid = 33 gid = 4
running postrotate script
* Reloading nginx configuration nginx
第三次运行:
reading config file /tmp/logrotate
extension is now .log
Handling 1 logs
rotating pattern: /var/log/nginx/access.log
/var/log/nginx/error.log 1 bytes (30 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/nginx/access.log
log needs rotating
considering log /var/log/nginx/error.log
log does not need rotating
rotating log /var/log/nginx/access.log, log->rotateCount is 30
Converted ' -%Y-%m-%d-%s' -> '-%Y-%m-%d-%s'
dateext suffix '-2021-08-27-1485508316'
glob pattern '-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
compressing log with: /bin/gzip
renaming /var/log/nginx/access.log to /var/log/nginx/access-2021-08-27-1485508316.log
creating new /var/log/nginx/access.log mode = 0544 uid = 33 gid = 4
running postrotate script
* Reloading nginx configuration nginx
状态文件的内容如下所示:
logrotate state -- version 2
"/var/log/nginx/error.log" 2021-08-27-9:0:0
"/var/log/nginx/access.log" 2021-08-27-9:11:56
下载 Linux logrotate 备忘单。
本文最初发表于作者个人博客,经许可改编。