【问题标题】:Log Rotation with Date Appended to Filename将日期附加到文件名的日志轮换
【发布时间】:2011-07-10 02:47:59
【问题描述】:

最初我认为使用 perl 的模块 Logfile:Rotate 可以轻松实现日志轮换,但现在要求更改为将日期附加到日志文件的文件名中。

我需要:

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

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

我愿意接受任何编程(脚本)语言,以便于日志轮换。 谁有更好的主意?

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

【问题讨论】:

    标签: scripting rotation logging


    【解决方案1】:

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

     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 提供的功能比编写代码来实现相同的功能要好得多。
    猜你喜欢
    • 1970-01-01
    • 2013-08-31
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多