简体   繁体   English

使用 DefaultRolloverStrategy 删除 log4j2 日志不起作用

[英]Deleting log4j2 logs with DefaultRolloverStrategy not working

I'm using log4j2 version 2.13.3 and I'm trying to delete old logs on application startup.我正在使用log4j2版本2.13.3 ,我试图在应用程序启动时删除旧日志。 I've tried various examples and they didn't even trigger my DefaultRolloverStrategy .我尝试了各种示例,它们甚至没有触发我的DefaultRolloverStrategy I implemented a configuration like this:我实现了这样的配置:

<properties>
    <property name="filePattern">${date:yyyy-MM-dd}</property>
    <Property name="baseDirectory">logs</Property>
</properties>

<Appenders>
<!-- ... -->
<RollingFile name="RollingFile" append="true" fileName="${baseDirectory}/${filePattern}.log"
         filePattern="${baseDirectory}/%d{yyyy-MM-dd}.log">
<PatternLayout>
    <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
    <OnStartupTriggeringPolicy/>
</Policies>
<DefaultRolloverStrategy>
    <Delete basePath="${baseDirectory}" maxDepth="1">
        <IfFileName glob="*.log"/>
        <IfLastModified age="10d"/>
    </Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>

<OnStartupTriggeringPolicy/> actually triggers my DefaultRolloverStrategy on startup but then the eligable files turn out to be empty: <OnStartupTriggeringPolicy/>实际上在启动时触发了我的DefaultRolloverStrategy但随后符合条件的文件结果为空:

As a consequence no logs are deleted but I'm fairly confident that my configuration is correct.因此,没有删除任何日志,但我相当有信心我的配置是正确的。 Is there anything I'm missing or why does the getEligibleFiles() method abort if my log file pattern doesn't match their strange PATTERN_COUNTER constant?如果我的日志文件模式与他们奇怪的PATTERN_COUNTER常量不匹配,我是否遗漏了什么或者为什么getEligibleFiles()方法会中止?

The scenario getEilligleFiles is accounting for is to determine which files to delete according to the rules specified on the DefaultRolloverStrategy. getEilligleFiles 考虑的场景是根据 DefaultRolloverStrategy 上指定的规则确定要删除哪些文件。 That strategy only deletes files within the timeframe of the current time pattern.该策略仅删除当前时间模式的时间范围内的文件。 For example, if you rollover every hour it will only purge files within the current hour.例如,如果您每小时滚动一次,它只会清除当前小时内的文件。 That is actually, the reason the delete action came to be - most people want to delete files in the whole directory or set of directories, not limited to the current "window".这实际上是删除操作的原因 - 大多数人希望删除整个目录或一组目录中的文件,而不仅限于当前“窗口”。

The "strange PATTERN_COUNTER constant" is there to check for counters expressed as %[0]n[n...]i in the file name. “奇怪的 PATTERN_COUNTER 常量”用于检查文件名中表示为 %[0]n[n...]i 的计数器。 That pattern causes the counter to be a fixed length with leading zeros.该模式导致计数器的长度固定并带有前导零。 You aren't using the SizeBasedTriggeringPolicy and so don't have the %i in your file pattern, so it won't match on that.您没有使用 SizeBasedTriggeringPolicy ,因此文件模式中没有 %i ,因此它不会匹配。

In other words, this code isn't doing anything because it doesn't apply to your configuration.换句话说,这段代码没有做任何事情,因为它不适用于您的配置。

When looking at DefaultRolloverStrategy you can see that its builder accepts custom Actions.查看 DefaultRolloverStrategy 时,您可以看到它的构建器接受自定义操作。 These are added during configuration and you should be able to see them if you have -Dlog4j2.debug enabled or have status=DEBUG set on the Configuration element in your Log4j2 config file.这些是在配置期间添加的,如果您启用了 -Dlog4j2.debug 或在 Log4j2 配置文件的 Configuration 元素上设置了 status=DEBUG,您应该能够看到它们。 You will also notice they are added to the RolloverDescription at the end of the rollover method.您还会注意到它们被添加到 rollover 方法末尾的 RolloverDescription 中。 This is passed back to RollingFileManager which does这将传递回 RollingFileManager

           if (success && descriptor.getAsynchronous() != null) {
                LOGGER.debug("RollingFileManager executing async {}", descriptor.getAsynchronous());
                asyncExecutor.execute(new AsyncAction(descriptor.getAsynchronous(), this));
                releaseRequired = false;
            }

near the end of the rollover method.接近翻转方法的结尾。 This will cause the Delete action will be kicked off as an asynchronous task at the end of every rollover.这将导致删除操作将在每次翻转结束时作为异步任务启动。

Again, enabling -Dlog4j2.debug or setting status=Debug should allow you to see these called.同样,启用 -Dlog4j2.debug 或设置 status=Debug 应该允许您看到这些调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM