Search code examples
javalog4jlog4j2

What does modulate = 'true' signify in Log4j TimeBased Triggering Policy


In the below example, a log file is created every day. With this example in mind, could you please provide an scenario to show the usage of modulate="true" with the interval set as 1.

<Configuration status="warn" name="MyApp" packages="">
    <Appenders>
        <RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%d{yyyy-MM-dd-HH}.log">
            <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interal=1 modulate="true"/>
            </Policies>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="error">
            <AppenderRef ref="RollingFile"/>
        </Root>
    </Loggers>
</Configuration>

Solution

  • When Log4j initializes it will calculate the rollover interval based on the %d pattern.

    • When modulate is false then the rollover will happen based on the time the application started. So if the application started at 1:41 pm and the smallest time interval is the hour then the next rollover will occur at 2:41 pm.
    • When modulate is true then the rollover will happen on "even" boundaries - so the next rollover would occur at 3pm.

    BTW - the XML in the example is invalid. It says interal=1 (the letter v is missing and the quotes around 1 are missing)...

    <TimeBasedTriggeringPolicy interal=1 modulate="true"/>
    

    ...but it SHOULD say interval="1" like so:

    <TimeBasedTriggeringPolicy interval="1" modulate="true"/>