gpt4 book ai didi

java - log4j2无法删除旧文件

转载 作者:行者123 更新时间:2023-12-05 06:37:29 25 4
gpt4 key购买 nike

我到处寻找答案。归档的文件数量超过 10 个后,我必须删除文件。

这是我的log4j2.xml

  <?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<RollingFile name="RollingFile" fileName="C:/temp/logs/app.log"
filePattern="C:/temp/logs/app.log.%i">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="1000 kb" />
</Policies>
<DefaultRolloverStrategy max="10">
<Delete basePath="C:/temp/logs/" maxDepth="1">
<IfFileName glob="app*.log*">
<IfLastModified age="2m">
<IfAny>
<IfAccumulatedFileCount exceeds="11" />
</IfAny>
</IfLastModified>
</IfFileName>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="RollingFile" />
</Root>
</Loggers>
</Configuration>

但它不会删除文件夹中的所有文件。例如在下面的屏幕截图中,它不会删除旧的日志文件:

logs

我已经阅读了所有文档并进行了大量讨论,但我无法解决这个问题。任何帮助将不胜感激。

最佳答案

这是一个与有人提出的类似问题,可能会为您尝试做的事情提供一些帮助 = Log4j2 - Configure RolloverStrategy to delete old log files .

注释应该解释 Delete 下的每个条件会做什么。在您的示例中,如果您希望删除 app-log-12-20*,那么您需要删除 IfLastModified 或 IfAccumulatedFileCount - 您将看到文件被删除

 <Configuration status="warn" name="MyApp" packages="">
<Appenders>
<RollingFile name="RollingFile" fileName="C:/temp/logs/app.log"
filePattern="C:/temp/logs/app.log.%i">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="1000 kb" />
</Policies>
<DefaultRolloverStrategy max="10">
<Delete basePath="C:/temp/logs/" maxDepth="1">
<IfAll>

<!-- Looks for file names starting with app*.log*. Any file including app.log.1 will satisfy this condition. Could delete current log app.log -->
<IfFileName glob="app*.log*" />
<!-- Looks for files that are older than 2 mins -->
<IfLastModified age="2m" />
<!-- This means there need to be 11 fails that satisfy the above conditions, that i.e. their name is app*log* and have time stamp greater than 2 mins. Keeps the most recent files that satisfy the condition -->
<IfAccumulatedFileCount exceeds="11" />
</IfAll>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="RollingFile" />
</Root>
</Loggers>

关于java - log4j2无法删除旧文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47914526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com