log4j2 RollingFile Appender requires date pattern

I have defined a RollingFile Appender in log4j2

<RollingFile name="Locserver" append="true" fileName="locserver.log" filePattern="locserver-%i.log">
    <PatternLayout>
        <pattern>%d{ISO8601} [%t] %p %c %L - %m%n</pattern>
    </PatternLayout>
    <Policies>
        <SizeBasedTriggeringPolicy size="50 MB"></SizeBasedTriggeringPolicy>
                <DefaultRolloverStrategy>10</DefaultRolloverStrategy>
    </Policies>
</RollingFile>

However when I try to run this I get an error

IllegalStateException : Pattern does not contain a date at org.apache.logging.log4j.core.appender.rolling.PatternProcessor.getNExtTime(PatternProcessor.java:91)

This goes away as soon as I put a date pattern in filePattern for example, locserver-%d{MM-dd-yyyy}-%i.log. But I dont want the date in the log names. Is a bug or something wrong with my config?


Solution 1:

Thanks Joe. I finally figured it out.

I had an empty TimeBasedTriggeringPolicy tag within my Policies list which was forcing the date check in the filePattern. Once I removed it, it started working fine.

Solution 2:

In RollingFile appender,TimeBasedTrigerringPolicy checks for datepattern in filepattern attribute of tag. Specifing datepattern is then mandatory when using TimeBasedTrigerringPolicy.

Place %d{...} containing a SimpleDate Pattern in filePattern, e.g.:

<Appenders>
    <RollingFile name="RollingFile" fileName="logs/rpi_gpio_latest.log"
                 filePattern="logs/**%d{yyyy-MM-dd_HH}**_rpi_gpio_%i.log">
        <PatternLayout>
            <Pattern>%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Pattern>
        </PatternLayout>
        <Policies>
            <TimeBasedTriggeringPolicy />
            <SizeBasedTriggeringPolicy size="4 MB"/>
        </Policies>
        <DefaultRolloverStrategy fileIndex="max"/>
    </RollingFile>
</Appenders>

Solution 3:

For me, that works like described here (using log4j 2.0-beta9 & Java 1.7).

My log4j.xml (size-policy set to 1kB just for testing):

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
  <Appenders>
    <Console name="STDOUT" target="SYSTEM_OUT">
      <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
    </Console>
    <RollingFile name="Logfile"
                 fileName="Log/App.log"
                 filePattern="Log/App-%i.log">
      <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
      <Policies>
        <SizeBasedTriggeringPolicy size="1 KB"/>
      </Policies>
      <DefaultRolloverStrategy max="4"/>
    </RollingFile>
  </Appenders>
  <Loggers>
    <Root level="info">
      <AppenderRef ref="STDOUT"/>
      <AppenderRef ref="Logfile"/>
    </Root>
  </Loggers>
</Configuration>

And it also works when using your config fragment (I'd just corrected the DefaultRolloverStrategy-setting):

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
  <Appenders>
    <Console name="STDOUT" target="SYSTEM_OUT">
      <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
    </Console>
    <RollingFile name="Locserver"
                 append="true"
                 fileName="locserver.log"
                 filePattern="locserver-%i.log">
      <PatternLayout>
          <pattern>%d{ISO8601} [%t] %p %c %L - %m%n</pattern>
      </PatternLayout>
      <Policies>
          <SizeBasedTriggeringPolicy size="50 MB"></SizeBasedTriggeringPolicy>
      </Policies>
      <DefaultRolloverStrategy max="4"/>
    </RollingFile>
  </Appenders>
  <Loggers>
    <Logger name="eeo.tts" level="debug"/>
    <Root level="info">
      <AppenderRef ref="STDOUT"/>
      <AppenderRef ref="Locserver"/>
    </Root>
  </Loggers>
</Configuration>

Which Version of log4j2 are you using?