0
votes

Log4j2 RollingFile appenders clashing

Below is a simplified debug version of our log4j2 configuration file (We rollover nightly not every minute!).
This configuration, instead of it creating a Rollover file each minute
(as per theTimeBasedTriggeringPolicy) will create one rollover file (non-tarred), containing JSON formatted logging, which will be overwritten every 20KB
(although it will end up being slightly greater than 20KB (See Screenshot).
We also get the following errors (abbreviated with "..."):-

2016-10-07 08:47:34,433 default-workqueue-4 ERROR Unable to copy file /.../logs/logFile-2016-10-07-08:47:11.log to /.../logs/logFile-2016-10-07-08:47:11.log: java.nio.file.NoSuchFileException /.../logs/logFile-2016-10-07-08:47:11.log

If we switch the order of the timeBasedRollingFileJsonLayout appender and the sizeBasedRollingFilePatternLayoutWithZippedArchive appender then no rollover occurs at all.

If we remove the sizeBasedRollingFilePatternLayoutWithZippedArchive appender then the timeBasedRollingFileJsonLayout appender works as expected.

We have the two different appenders for different environments, where the logs may or may not be hooked up to ELK.
In our real log4j2 config file we use properties to select the appropriate appender for the environment.
I have removed the properties from this file for clarity and to rule them out as a possible cause of the problem.

We are using log4j 2.6.2.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %logger{36} - %msg %n" /> 
        </Console>
        <RollingFile name="timeBasedRollingFileJsonLayout" append="true" fileName="logs/logFile.log" filePattern="logs/logFile-%d{yyyy-MM-dd-HH:mm}.log">
            <JSONLayout properties="true" compact="true" eventEol="true" />
            <Policies>
                <TimeBasedTriggeringPolicy />
            </Policies>
        </RollingFile>
        <RollingFile name="sizeBasedRollingFilePatternLayoutWithZippedArchive" append="true"  fileName="logs/logFile.log"  filePattern="logs/logFile-%d{yyyy-MM-dd}-%i.log.gz">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %logger{36} - %msg %n" /> 
            <Policies>
                <SizeBasedTriggeringPolicy size="20KB" />
            </Policies>
        </RollingFile>
    </Appenders>
    <Loggers>
        <logger name="logger.one" level="info" additivity="false">
            <AppenderRef ref="timeBasedRollingFileJsonLayout" />
        </logger>
        <Logger name="logger.two" level="info" additivity="false">
            <AppenderRef ref="timeBasedRollingFileJsonLayout" />
        </Logger>
        <Logger name="logger.three" level="info" additivity="false">
            <AppenderRef ref="timeBasedRollingFileJsonLayout" />
        </Logger>
        <Root level="info">
            <AppenderRef ref="timeBasedRollingFileJsonLayout" level="all" />
        </Root>
    </Loggers>
</Configuration>
1

1 Answers

2
votes

I am at a loss to understand why you think this should work. You have two appenders trying to write to the same file trying to rollover based on different criteria and rollover to files with different names. It is no surprise that you are getting the file is in use error since two things have it open at once.