导读:使用此 Linux 命令保持日志文件更新。                                       
本文字数:6614,阅读时长大约:7分钟
https://linux.cn/article-13909-1.html

作者:Ayush Sharma
译者:perfiffer
日志非常适合找出应用程序在做什么或对可能的问题进行故障排除。几乎我们处理的每个应用程序都会生成日志,我们希望我们自己开发的应用程序也生成日志。日志越详细,我们拥有的信息就越多。但放任不管,日志可能会增长到无法管理的大小,反过来,它们可能会成为它们自己的问题。因此,最好将它们进行裁剪,保留我们需要的那些,并将其余的归档。
基本功能
logrotate 实用程序在管理日志方面非常出色。它可以轮转日志、压缩日志、通过电子邮件发送日志、删除日志、归档日志,并在你需要时开始记录最新的。
运行 logrotate 非常简单——只需要运行 logrotate -vs state-file config-file。在上面的命令中,v 选项开启详细模式,s 指定一个状态文件,最后的 config-file 是配置文件,你可以指定需要做什么。
实战演练
让我们看看在我们的系统上静默运行的 logrotate 配置,它管理我们在 /var/log 目录中找到的大量日志。查看该目录中的当前文件。你是否看到很多 *.[number].gz 文件?这就是 logrotate 正在做的。你可以在 /etc/logrotate.d/rsyslog 下找到此配置文件。我的配置文件如下:
  1. /var/log/syslog
  2. {
  3.         rotate 7
  4.         daily
  5.         missingok
  6.         notifempty
  7.         delaycompress
  8.         compress
  9.         postrotate
  10.                 reload rsyslog >/dev/null2>&1||true
  11.         endscript
  12. }
  13. /var/log/mail.info
  14. /var/log/mail.warn
  15. /var/log/mail.err
  16. /var/log/mail.log
  17. /var/log/daemon.log
  18. /var/log/kern.log
  19. /var/log/auth.log
  20. /var/log/user.log
  21. /var/log/lpr.log
  22. /var/log/cron.log
  23. /var/log/debug
  24. /var/log/messages
  25. {
  26.         rotate 4
  27.         weekly
  28.         missingok
  29.         notifempty
  30.         compress
  31.         delaycompress
  32.         sharedscripts
  33.         postrotate
  34.                 reload rsyslog >/dev/null2>&1||true
  35.         endscript
  36. }
该文件首先定义了轮转 /var/log/syslog 文件的说明,这些说明包含在后面的花括号中。以下是它们的含义:
◈ rotate 7: 保留最近 7 次轮转的日志。然后开始删除超出的。
◈ daily: 每天轮转日志,与 rotate 7 一起使用,这意味着日志将保留过去 7 天。其它选项是每周、每月、每年。还有一个大小参数,如果日志文件的大小增加超过指定的限制(例如,大小 10k、大小 10M、大小 10G 等),则将轮转日志文件。如果未指定任何内容,日志将在运行 logrotate 时轮转。你甚至可以在 cron 中运行 logrotate 以便在更具体的时间间隔内使用它。
◈ missingok: 如果日志文件缺失也没关系。不要惊慌。
◈ notifempty: 日志文件为空时不轮转。
◈ compress: 开启压缩,使用 nocompress 关闭它。
◈ delaycompress: 如果压缩已打开,则将压缩延迟到下一次轮转。这允许至少存在一个轮转但未压缩的文件。如果你希望昨天的日志保持未压缩以便进行故障排除,那么此配置会很有用。如果某些程序在重新启动/重新加载之前可能仍然写入旧文件,这也很有帮助,例如 Apache。
◈ postrotate/endscript: 轮转后运行此部分中的脚本。有助于做清理工作。还有一个 prerotate/endscript 用于在轮转开始之前执行操作。
你能弄清楚下一节对上面配置中提到的所有文件做了什么吗?第二节中唯一多出的参数是 sharedscripts,它告诉 logrotate 在所有日志轮转完成之前不要运行 postrotate/endscript 中的部分。它可以防止脚本在每一次轮转时执行,只在最后一次轮转完成时执行。
看点新的东西
我使用下面的配置来处理我系统上的 Nginx 的访问和错误日志。
  1. /var/log/nginx/access.log
  2. /var/log/nginx/error.log  {
  3. size1
  4.         missingok
  5.         notifempty
  6.         create 544 www-data adm
  7.         rotate 30
  8.         compress
  9.         delaycompress
  10.         dateext
  11.         dateformat -%Y-%m-%d-%s
  12.         sharedscripts
  13.         extension .log
  14.         postrotate
  15.                 service nginx reload
  16.         endscript
  17. }
上面的脚本可以使用如下命令运行:
  1. logrotate -vs state-file/tmp/logrotate
第一次运行该命令会给出以下输出:
  1. reading config file/tmp/logrotate
  2. extension is now .log
  3. Handling1 logs
  4. rotating pattern:/var/log/nginx/access.log
  5. /var/log/nginx/error.log   1 bytes (30 rotations)
  6. empty log files are not rotated, old logs are removed
  7. considering log /var/log/nginx/access.log
  8.   log needs rotating
  9. considering log /var/log/nginx/error.log
  10.   log does not need rotating
  11. rotating log /var/log/nginx/access.log, log->rotateCount is30
  12. Converted' -%Y-%m-%d-%s'->'-%Y-%m-%d-%s'
  13. dateext suffix '-2021-08-27-1485508250'
  14. 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]'
  15. glob finding logs to compress failed
  16. glob finding old rotated logs failed
  17. renaming /var/log/nginx/access.log to /var/log/nginx/access-2021-08-27-1485508250.log
  18. creating new/var/log/nginx/access.log mode =0544 uid =33 gid =4
  19. running postrotate script
  20. *Reloading nginx configuration nginx
第二次运行它:
  1. reading config file/tmp/logrotate
  2. extension is now .log
  3. Handling1 logs
  4. rotating pattern:/var/log/nginx/access.log
  5. /var/log/nginx/error.log   1 bytes (30 rotations)
  6. empty log files are not rotated, old logs are removed
  7. considering log /var/log/nginx/access.log
  8.   log needs rotating
  9. considering log /var/log/nginx/error.log
  10.   log does not need rotating
  11. rotating log /var/log/nginx/access.log, log->rotateCount is30
  12. Converted' -%Y-%m-%d-%s'->'-%Y-%m-%d-%s'
  13. dateext suffix '-2021-08-27-1485508280'
  14. 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]'
  15. compressing log with:/bin/gzip
  16. renaming /var/log/nginx/access.log to /var/log/nginx/access-2021-08-27-1485508280.log
  17. creating new/var/log/nginx/access.log mode =0544 uid =33 gid =4
  18. running postrotate script
  19. *Reloading nginx configuration nginx
第三次运行它:
  1. reading config file/tmp/logrotate
  2. extension is now .log
  3. Handling1 logs
  4. rotating pattern:/var/log/nginx/access.log
  5. /var/log/nginx/error.log   1 bytes (30 rotations)
  6. empty log files are not rotated, old logs are removed
  7. considering log /var/log/nginx/access.log
  8.   log needs rotating
  9. considering log /var/log/nginx/error.log
  10.   log does not need rotating
  11. rotating log /var/log/nginx/access.log, log->rotateCount is30
  12. Converted' -%Y-%m-%d-%s'->'-%Y-%m-%d-%s'
  13. dateext suffix '-2021-08-27-1485508316'
  14. 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]'
  15. compressing log with:/bin/gzip
  16. renaming /var/log/nginx/access.log to /var/log/nginx/access-2021-08-27-1485508316.log
  17. creating new/var/log/nginx/access.log mode =0544 uid =33 gid =4
  18. running postrotate script
  19. *Reloading nginx configuration nginx
状态文件的内容如下所示:
  1. logrotate state -- version 2
  2. "/var/log/nginx/error.log"2021-08-27-9:0:0
  3. "/var/log/nginx/access.log"2021-08-27-9:11:56
◈ 下载 Linux logrotate 备忘单🔗 opensource.com
本文首发于作者个人博客🔗 notes.ayushsharma.in,经授权改编。

作者:Ayush Sharma 选题:lujun9972 译者:perfiffer 校对:wxy
本文由 LCTT 原创编译,Linux中国 荣誉推出
欢迎遵照 CC-BY-NC-SA 协议规定转载,
如需转载,请在文章下留言 “转载:公众号名称”,
我们将为您添加白名单,授权“转载文章时可以修改”。
继续阅读
阅读原文