java 如何使用 log4j2 删除旧日志
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33237731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete old logs with log4j2
提问by obanadingyo
( F.Y.I. I already searched out many documents in Internet. I'm using storm-0.10.0-beta1. Configuration file of log4j2 in Storm is worker.xml )
(仅供参考,我已经在互联网上搜索了很多文档。我使用的是storm-0.10.0-beta1。Storm中log4j2的配置文件是worker.xml)
Now, I try to use log4j2.
现在,我尝试使用 log4j2。
I'm searching out way of deleting old logs but I cannot find out. Part of configuration is like below.
我正在寻找删除旧日志的方法,但我找不到。部分配置如下。
<RollingFile name="SERVICE_APPENDER"
fileName="${sys:storm.home}/logs/${sys:logfile.name}.service"
filePattern="${sys:storm.home}/logs/${sys:logfile.name}.service.%d{yyyyMMdd}">
<PatternLayout>
<pattern>${pattern}</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
<DefaultRolloverStrategy max="9"/>
</RollingFile>
At first, I expected that log files which are older than 3 days are removed.
起初,我希望删除超过 3 天的日志文件。
But, actually, it doesn't.
但是,实际上,它没有。
So, I wonder whether there is a way to remove old logs or not.
所以,我想知道是否有办法删除旧日志。
If there is a way which I didn't catch yet, please notify me.
如果有我还没有抓住的方法,请通知我。
回答by Remko Popma
Since 2.5, Log4j supports a custom Delete actionthat is executed on every rollover.
从 2.5 开始,Log4j 支持在每次翻转时执行的自定义删除操作。
You can control which files are deleted by any combination of:
您可以通过以下任意组合控制删除哪些文件:
- Name (matching a globor a regex)
- Age("delete if 14 days old or older")
- Count ("keep only the most recent 3")
- Size ("keep only the most recent files up to 500MB")
Users who need even more fine-grained control over which files to delete can specify a script condition using any supported JSR-223 scripting language.
需要更精细地控制要删除哪些文件的用户可以使用任何受支持的 JSR-223 脚本语言指定脚本条件。
Please check out the documentation, it has three full examples that may be useful.
请查看文档,它包含三个可能有用的完整示例。
For your question, this snippet should work:
对于您的问题,此代码段应该有效:
<DefaultRolloverStrategy>
<!--
* only files in the log folder, no sub folders
* only rolled over log files (name match)
* only files that are 4 days old or older
-->
<Delete basePath="${sys:storm.home}/logs/" maxDepth="1">
<IfFileName glob="*.service.????????" />
<IfLastModified age="4d" />
</Delete>
</DefaultRolloverStrategy>
Finally, be careful! There is no way to recover files deleted this way. :-)
最后,小心!无法恢复以这种方式删除的文件。:-)
回答by Marged
You can find more background information in this JIRA entry for log4j:
您可以在 log4j 的 JIRA 条目中找到更多背景信息:
https://issues.apache.org/jira/browse/LOG4J2-524
https://issues.apache.org/jira/browse/LOG4J2-524
It seems to be the case that auto deleting old log files does not work when you only use a TimeBasedTriggeringPolicy
当您仅使用 TimeBasedTriggeringPolicy