简体   繁体   English

log4j2 RollingFile 只保留 4 天

[英]log4j2 RollingFile only keeping 4 days

I tried setting up log4j to compress my log files and send them to a mount.我尝试设置 log4j 来压缩我的日志文件并将它们发送到挂载。 I added a day/month/year to it in an attempt to get it in directories like that.我在其中添加了日/月/年,试图将其放入这样的目录中。

At first this appeared to be working.起初这似乎有效。

Coming back a while later, it turns out only the most recent 4 days are present.一段时间后回来,结果只有最近的 4 天存在。

This is my config:这是我的配置:

<Configuration>
    <Appenders>

        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout>
                <Pattern>
                    %d{yyyy-MM-dd HH:mm:ss.SSS} %-5p (%t) %c{1.} %m%n
                </Pattern>
            </PatternLayout>
        </Console>

        <RollingFile
                name="RollingFile"
                fileName="$/home/myservice/myservice.log"
                filePattern="/mnt/logbackups/$${date:dd-MM-yyyy}/myservice-%d{dd-MM-yyyy}-%i.log.gz" >
            <PatternLayout>
                <Pattern>
                    %d{yyyy-MM-dd HH:mm:ss.SSS} %-5p (%t) %c{1.} %m%n
                </Pattern>
            </PatternLayout>
            <Policies>
                <OnStartupTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="32 MB"/>
            </Policies>
        </RollingFile>
</Configuration>

I tried looking for a cause in the documentation: https://logging.apache.org/log4j/2.x/manual/appenders.html我试着在文档中寻找原因: https://logging.apache.org/log4j/2.x/manual/appenders.html

But I can't find anything.但我找不到任何东西。 Then again it's a gigantic page, maybe I'm missing something.然后又是一个巨大的页面,也许我遗漏了什么。

Does anybody know why this is happening?有人知道为什么会这样吗?

You defined the RollingFileAppender which will regularly write to a new logfile.您定义了将定期写入新日志文件的 RollingFileAppender。 You also defined the triggers when to write to a new file, but you did not set the Rollover Strategies .您还定义了何时写入新文件的触发器,但您没有设置Rollover Strategies

  • You could use the DirectWrite Rollover Strategy with maxFiles not set (to a value greater than zero).您可以在未设置 maxFiles 的情况下使用 DirectWrite Rollover Strategy(设置为大于零的值)。

  • You could also try to configure the Log Archive Retention Policy/Delete on Rollover action.您还可以尝试配置日志存档保留策略/更新时删除操作。 Set testMode=true to prevent any file from being deleted.设置 testMode=true 以防止删除任何文件。

Examples are available on the huge page you mentioned.您提到的大页面上提供了示例。

What you looking for is the "Default Rollover Strategy" specified under Rollover Policies .您正在寻找的是Rollover Policies下指定的“默认滚动策略”。 You can add one explicitly to adjust the default parameters.您可以显式添加一个以调整默认参数。

Specify it like this inside the RollingFile:在 RollingFile 中这样指定它:

<DefaultRolloverStrategy max="20"/>

Although I am confused because according to their documentation the default is 7:尽管我很困惑,因为根据他们的文档,默认值为 7:

max |最大 | integer | integer | The maximum value of the counter.计数器的最大值。 Once this values is reached older archives will be deleted on subsequent rollovers.一旦达到此值,较旧的存档将在后续更新时被删除。 The default value is 7默认值为 7

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

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