Galet
Galet

Reputation: 6289

How to write integration test for log rollover on log4j2

I am using log4j2 for logging in my application. I am using following policies in my log4.xml

<Policies>
<SizeBasedTriggeringPolicy size="2000 MB" />
<TimeBasedTriggeringPolicy />
</Policies>

and also rotate logs when server shutdown.

How can I write integration test to verify log rotation works based on above conditions ?

Upvotes: 1

Views: 751

Answers (1)

asch
asch

Reputation: 1963

Your test should print bulk of messages to cause the log rolling. In order to help it happen (to rolling), use the dedicated log4j2 configuration for test. Define different values for policies: reduce a size policy to the rather small size (much less that 2000 MB) and change time pattern and time policy interval in a way, that allows creating of several files for the same date. The example of configuration below defines creation of up to 21 daily files, file with a lowest index is the newest. File rolling is done upon reaching a size of 1k:

<RollingFile name="AppLog" fileName="${logDir}/${logFile}.log"
        filePattern="${logDir}/${logFile}-%d{dd-MM-yyyy}-%i.log">
                <Policies>
            <TimeBasedTriggeringPolicy/>
            <SizeBasedTriggeringPolicy size="1 KB"/>
        </Policies>
        <DefaultRolloverStrategy fileIndex="min" max="21"/>
</RollingFile>

I have a working example of this on git here. Look for the test class LogApplicationTest. If you need more tips or have another issues - see my blog.

Upvotes: 0

Related Questions