Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4j2 RollingFile Appender requires date pattern

Tags:

log4j2

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?

like image 862
sanky Avatar asked Sep 17 '25 11:09

sanky


2 Answers

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>
like image 156
reena Avatar answered Sep 23 '25 12:09

reena


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.

like image 43
sanky Avatar answered Sep 23 '25 13:09

sanky