简体   繁体   中英

log4j2 RollingFile only keeping 4 days

I tried setting up log4j to compress my log files and send them to a mount. 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.

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

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. You also defined the triggers when to write to a new file, but you did not set the Rollover Strategies .

  • You could use the DirectWrite Rollover Strategy with maxFiles not set (to a value greater than zero).

  • 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.

Examples are available on the huge page you mentioned.

What you looking for is the "Default Rollover Strategy" specified under Rollover Policies . You can add one explicitly to adjust the default parameters.

Specify it like this inside the RollingFile:

<DefaultRolloverStrategy max="20"/>

Although I am confused because according to their documentation the default is 7:

max | 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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