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

1

5

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>
Frivolous answered 22/6, 2020 at 14:58 Comment(0)
H
11

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"/>
Hel answered 23/6, 2020 at 16:31 Comment(1)
I wonder whether this is still accurate. According to the current source code Log4j sets smaller time units to zero, so with interval="3" rollover will occur at 16:00 with modulate="false" and 15:00 with modulate="true".Fowle

© 2022 - 2024 — McMap. All rights reserved.