logrotate dateformat 不支持毫秒扩展

logrotate dateformat does not support milliseconds extension

我正在使用 logrotate。它适用于 dateformat %s 并在 somefile.log.1555267419.gz 中生成文件。但我需要添加以毫秒为单位的扩展日期格式(somefile.log.1555267419789.gz)。

我查看了联机帮助页,据我所知,它说它不支持毫秒说明符。

有什么方法可以在扩展中添加毫秒并且仍然能够轮换旧日志文件吗?

/var/log/somelog/*.log {
   compress
   notifempty
   daily
   copytruncate
   size 15M
   dateext
   dateformat .%s
   rotate 20
}

正如您已经提到的,logrotate docs 提到不支持毫秒。您可以使用 %s 获取 EPOCH 时间,假设系统时钟是 2001 年 9 月 9 日之后。

dateformat format_string

Specify the extension for dateext using the notation similar to strftime(3) function. Only %Y %m %d and %s specifiers are allowed. The default value is -%Y%m%d. Note that also the character separating log name from the extension is part of the dateformat string. The system clock must be set past Sep 9th 2001 for %s to work correctly. Note that the datestamps generated by this format must be lexically sortable (i.e., first the year, then the month then the day. e.g., 2001/12/01 is ok, but 01/12/2001 is not, since 01/11/2002 would sort lower while it is later). This is because when using the rotate option, logrotate sorts all rotated filenames to find out which logfiles are older and should be removed.

但是,您可以尝试使用 logrotate scripts 手动设置毫秒粒度的 EPOCH。这是一个示例,您调用位于 /my/script 的自定义脚本,该脚本将旋转的文件名作为参数。在 prerotate/postrotate 中使用 </code> 可以获得正在旋转的文件的绝对路径。您还想使用 <code>nosharedscripts 配置(默认设置)以确保 prerotate/postrotate 脚本是 运行 每个日志文件匹配您的 *.log 模式。

/var/log/somelog/*.log {
   compress
   notifempty
   daily
   copytruncate
   size 15M
   dateext
   dateformat .%s
   rotate 20
   nosharedscripts
   postrotate
       /my/bash/script  > /dev/null
   endscript
}

在您的自定义脚本中,您可以通过重命名来设置文件名的毫秒数。在 Linux 机器上以毫秒获取 EPOCH 的一种方法是 运行 date "+%s%3N".