Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does RollingFileAppender work with log4j2?

I'm use to RollingFileAppender on normal log4j. Now I'm switching to log4j2, and cannot get the appender to work.

The File appender below works as expected. But the logging file for RollingFile is never created. Why?

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Appenders>
        <File name="FILE" fileName="c:/logs.log">
            <PatternLayout pattern="%d %p %c: %m%n" />
        </File>

        <RollingFile name="ROLLING" fileName="c:/logsroll.log">
            <PatternLayout pattern="%d %p %c: %m%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="0.001 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="FILE" />
            <AppenderRef ref="ROLLING" />
        </Root>
    </Loggers>
</Configuration>
like image 357
membersound Avatar asked Jan 07 '14 15:01

membersound


People also ask

What is rollover strategy in Log4j2?

The DefaultRolloverStrategy is a combination of a time-based policy and a fixed-window policy. When the file name pattern contains a date format then the rollover time interval will be used to calculate the time to use in the file pattern.

What is root logger in Log4j2?

This concept is known as Logger Hierarchy. Logger Hierarchy is made up of set of LoggerConfig objects with a parent-child relationship. The topmost element in every Logger Hierarchy is the Root Logger. If Log4j2 doesn't find the configuration file, only Root Logger will be used for logging with logging level as ERROR.


1 Answers

The RollingFile tag is missing a filePattern attribute.

<RollingFile name="ROLLING" 
             fileName="c:/logsroll.log"
             filePattern="c:/logsroll-%i.log">
like image 58
Joe Avatar answered Oct 20 '22 18:10

Joe