M.I
M.I

Reputation: 303

log4j2 unlimited RollingFile

I'm using log4j2 and trying to log with log-rotation. Specifically, I want to log at the maximum size of 10MB and rotate unlimitedly. The configuration below generates 3 generations of rolling files because "DefaultRolloverStrategy max" is set to 3. Could you please, guide me how to log unlimited number of files with at the maximum size of 10MB?

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Properties>
        <Property name="format1">%m%n</Property>
        <Property name="logfile">${sys:logDirectory}/log.log</Property>
        <Property name="logfile-archive">${sys:logDirectory}/log_%d{yyyy-MM-dd}.%i.log
        </Property>
    </Properties>
    <Appenders>
        <RollingFile name="logfile001" append="true" fileName="${logfile}"
            filePattern="${logfile-archive}">
            <PatternLayout>
                <pattern>${format1}</pattern>
            </PatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="10MB" />
            </Policies>
            <DefaultRolloverStrategy max="3" />
        </RollingFile>
    </Appenders>

    <Loggers>
        <Root level="trace">
            <AppenderRef ref="logfile001" />
        </Root>
    </Loggers>
</Configuration>

Upvotes: 2

Views: 3388

Answers (1)

sazzad
sazzad

Reputation: 6267

Set an extreme value to DefaultRolloverStrategy max. E.g.

<DefaultRolloverStrategy max="1000000000" />

Update:

According to Log4j2 documentation, as of release 2.8, it can be done by setting fileIndex attribute to nomax. E.g.

<DefaultRolloverStrategy fileIndex="nomax" />

Upvotes: 6

Related Questions