文件名附加日期的日志轮换

发布于 2024-10-20 18:10:02 字数 497 浏览 1 评论 0原文

最初我认为使用 perl 的模块 Logfile:Rotate 进行日志轮转会很容易,但现在要求更改为将日期附加到日志文件的文件名中。

我需要:

  1. 附加(每天)旋转的文件名和日期。
  2. 压缩 10 天以上的日志文件。
  3. 删除超过 30 天的日志文件。
  4. 要在目录(即../logs)中旋转的日志

我尝试了Python的 logging .handlers.TimedRotatingFileHandler,但它会在 cron 执行时创建新的空日志文件。我认为这意味着一个连续的过程,而不是每个间隔的 cron 执行。

我对任何编程(脚本)语言持开放态度,以便轻松进行日志轮换。 有人有更好的想法吗?

并不是要索要代码。只是建议。

Originally I thought log rotation would be easy with the perl's module Logfile:Rotate, but now the requirement changed to appending the date to the filename of the log file.

I need to:

  1. Append the filename rotated (daily) with date.
  2. Zip the log files older than 10 days.
  3. Delete log files older than 30 days.
  4. Logs to be rotated in a directory (i.e. ../logs)

I tried Python's logging.handlers.TimedRotatingFileHandler, but it creates new empty log files on cron execution. I think it's meant for a process that's continuous and not per interval cron execution.

I'm open to any programming (scripting) language for easy log rotation.
Anyone got better ideas?

Not meant to ask for code. Just suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一笔一画续写前缘 2024-10-27 18:10:02

目前大多数 *nix 发行版上的 logrotate 实用程序支持您的以下选项兴趣:

 compress
          Old versions of log files are compressed with gzip by default. See also nocompress.

 dateext
          Archive old versions of log files adding a daily extension like YYYYMMDD instead of simply adding a number.

 olddir directory
          Logs are moved into directory for rotation. The directory must be on the same physical device as the log file being
          rotated, and is assumed to be relative to the directory holding the log file unless an absolute path name is speci-
          fied.  When  this  option is used all old versions of the log end up in directory.  This option may be overriden by
          the noolddir option.

 notifempty
          Do not rotate the log if it is empty (this overrides the ifempty option).

 postrotate/endscript
          The lines between postrotate and endscript (both of which must appear on lines by themselves)  are  executed  after
          the  log file is rotated. These directives may only appear inside of a log file definition.  See prerotate as well.

logrotate utility on most of *nix distros available today support following options of your interest:

 compress
          Old versions of log files are compressed with gzip by default. See also nocompress.

 dateext
          Archive old versions of log files adding a daily extension like YYYYMMDD instead of simply adding a number.

 olddir directory
          Logs are moved into directory for rotation. The directory must be on the same physical device as the log file being
          rotated, and is assumed to be relative to the directory holding the log file unless an absolute path name is speci-
          fied.  When  this  option is used all old versions of the log end up in directory.  This option may be overriden by
          the noolddir option.

 notifempty
          Do not rotate the log if it is empty (this overrides the ifempty option).

 postrotate/endscript
          The lines between postrotate and endscript (both of which must appear on lines by themselves)  are  executed  after
          the  log file is rotated. These directives may only appear inside of a log file definition.  See prerotate as well.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文