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?
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With