Question

I am using the following log4j2.xml file:

<configuration>
    <appenders>
        <RollingFile name="logfile" fileName="C:/opt/log/views/views.log"
                     filePattern="C:/opt/log/views/views.%d{yyyy-ww}.log.gz">
            <Policies>
                <TimeBasedTriggeringPolicy />
            </Policies>
            <DefaultRolloverStrategy max="3" />
            <PatternLayout pattern="%d{ISO8601} VIEWS[%p][%c]...." />
        </RollingFile>
    </appenders>
    <loggers>
        <root level="TRACE">
            <appender-ref ref="logfile" />
        </root>
    </loggers>
</configuration>

When I tail the file and start the server, I get one log message a time. It's not appending, but instead replacing the only line in the file with each new line. I'm a bit baffled by this and hoping someone has seen this before. Thanks in advance.

Was it helpful?

Solution

As far as I can tell, there's nothing wrong with your configuration (at least, on my machine it works as expected).

Maybe the problem is in the pattern:

%d{ISO8601} VIEWS[%p][%c]....

You miss a %n (newline) at the end, so everything is effectively written on the same line.

OTHER TIPS

Spent some time with exactly the same problem. I am convinced now that the biggest unit of time that the TimeBasedTriggeringPolicy in log4j2 (beta9) can deal with is a day, not (like in your configuration) a week or a month.

Test it: Change your configuration to

 <RollingFile name="logfile" fileName="C:/opt/log/views/views.log"
     filePattern="C:/opt/log/views/views.%d{yyyy-MM-dd}.log.gz">

and it should magically work.

This has already been reported as a bug here, but is (as of today) not fixed: https://issues.apache.org/jira/browse/LOG4J2-385

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top