Phi*_*ord 5 regex logging logrotate
我有一个自定义日志记录类,它为进程的每个实例创建一个日志,并为日志文件名添加一个唯一的 id,例如:
我也可以添加日期/时间戳,例如:
LogRotate 是否能够使用正则表达式,以便我可以按日期和/或进程 ID 归档我的日志文件?
小智 5
我不知道以后的版本是否支持,但经过进一步研究后,我发现支持 shell 通配符。两者*(匹配多个字符)和?(匹配单个字符)可以被组合,以匹配特定的文件。
例如,这里有与您的用例相匹配的模式以及可以在以下位置找到的文件的其余部分 /etc/logrotate.d/process
/path/to/my/logfiles/process.????.????????.log
/path/to/my/logfiles/process.????.log
{
# Look for previously matched log files and rotate daily if found
daily
# use date as a suffix of the rotated file
dateext
# Compress log file, optional if the files are small enough
compress
# Allow for a log file pattern to NOT match in order to support both
# filename formats
missingok
# Do not create replacement log files, the application will do that
nocreate
# Keep 30 days worth of rotated logs
maxage 30
}
Run Code Online (Sandbox Code Playgroud)
但是,如果您打算process成为实际进程 ID 的占位符,那么我相信您可以使用通配符代替进程 ID 号,如下所示:
/path/to/my/logfiles/*.????.????????.log
/path/to/my/logfiles/*.????.log
{
...
}
Run Code Online (Sandbox Code Playgroud)
我希望这有帮助。我也去寻找一种可行的正则表达式方法,最终决定使用 shell 通配符,而不是我认为可行但更复杂的解决方案。
一个例子:
在搜索正则表达式解决方案时,我发现了一篇题为“从 logrotate globbing 匹配中排除文件”的博客文章,它提供了以下解决方案:
/var/log/upstart/*.log {
daily
missingok
rotate 7
compress
notifempty
nocreate
nosharedscripts
prerotate
bash -c "[[ ! $1 =~ testprogram ]]"
endscript
}
Run Code Online (Sandbox Code Playgroud)
我最终从该示例中提取了两个项目并稍微修改了匹配。在我的情况下,我想旋转目录中的所有文件,除了一些具有.inp扩展名的输入文件。
这就是我想出的:
# Force the prerotate "script" below to be run on each individual file
# in order to verify that it isn't an unprocessed input file
nosharedscripts
# Skip rotating any unprocessed input files (*.inp extension)
prerotate
bash -c "[[ ! $1 =~ \.inp$ ]]"
endscript
Run Code Online (Sandbox Code Playgroud)
据logrotate -d /etc/logrotate.d/myfilename它似乎工作。然而,正如我所提到的,我选择了 shell 通配符方法,因为后面的人似乎更容易维护。
| 归档时间: |
|
| 查看次数: |
13598 次 |
| 最近记录: |