16

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>
MartyIX
  • 27,828
  • 29
  • 136
  • 207
membersound
  • 81,582
  • 193
  • 585
  • 1,120

2 Answers2

28

The RollingFile tag is missing a filePattern attribute.

<RollingFile name="ROLLING" 
             fileName="c:/logsroll.log"
             filePattern="c:/logsroll-%i.log">
Joe
  • 3,090
  • 6
  • 37
  • 55
  • 2
    Correct. You need the filePattern attribute so that the rollover mechanism knows how to rename the old log files (and potentially zip them). – Remko Popma Jan 07 '14 at 23:47
  • 7
    Additionally, if you add attribute `status="trace"` to the top `` element (``), log4j2 debug info will be printed to the console to help you troubleshoot any configuration issues. – Remko Popma Jan 07 '14 at 23:57
  • Any guess how can I use this attribute in a configuration file and not in a XML config? – рüффп Feb 24 '15 at 16:42
  • What exactly means `configuration file` if it not means `XML config`? ([See here](http://stackoverflow.com/questions/28235021/log4j2-on-android/28453604#28453604) for an approach for a configuration without configuration file.) – Joe Feb 25 '15 at 11:11
  • so the parent folder (c:/ in here) need to be specified twice? both in fileName and filePattern? what if they are specified different? – Kalpesh Soni Apr 30 '15 at 15:12
  • Why both `fileName` and `filePattern`!? I know the documentation lists both. – lcfd Apr 16 '21 at 08:35
8

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>
Alireza Fattahi
  • 42,517
  • 14
  • 123
  • 173