日志旋转,日期附加到文件名

时间:2011-03-07 05:03:09

标签: scripting rotation logging

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

我需要:

  1. 使用日期附加旋转(每日)文件名。
  2. 压缩超过10天的日志文件。
  3. 删除超过30天的日志文件。
  4. 记录在目录中旋转(即../ logs)
  5. 我尝试了Python的logging.handlers.TimedRotatingFileHandler,但它在cron执行时创建了新的空日志文件。我认为这意味着一个连续的进程而不是每个时间间隔的cron执行。

    我愿意使用任何编程(脚本)语言来轻松进行日志轮换。 有人有更好的想法吗?

    不是要求代码。只是建议。

1 个答案:

答案 0 :(得分:5)

今天可用的大多数* 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.