Question

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>
Was it helpful?

Solution

The RollingFile tag is missing a filePattern attribute.

<RollingFile name="ROLLING" 
             fileName="c:/logsroll.log"
             filePattern="c:/logsroll-%i.log">

OTHER TIPS

I used log4j2 version 2.0, in some cases it throws error if you do not set any date in file pattern, in this case you can use some thing like below:

      <RollingFile name="MyFile" fileName="d:/log/bsi/admin/total/totalLog.log"
            filePattern="d:/log/totalLog-%d{MM-dd-yyyy}-%i.log">
            <PatternLayout>
                <Pattern>%d %p %c [%t] %m%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="1 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="2000"/>
        </RollingFile>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top