I have a java app that uses log4j for logging. Everything was good until the log4Shell scare and I was forced to upgrade to 2.17.1 .
My app uses startup, time and size based rollover strategies. Normally it will produce about 6 log files per day. But some days when things go really crazy (and I need those log files the most), the number of files may exceed 100 in a day. Program keeps crashing, a watchdog keeps waking it up.
The version that ran before, with nothing changed but log4j version, started overwriting the last log file after a fixed number, probably 100. I started using %04i in the file name so I got good readable cycle numbers. My current config doesn't trash the last file any more, but this happens:
-rw-r--r-- 1 xxxxxxx xx 588 Mar 8 17:08 TRACE.2022-03-08-17.0096.log
-rw-r--r-- 1 xxxxxxx xx 213 Mar 8 17:08 TRACE.2022-03-08-17.0097.log
-rw-r--r-- 1 xxxxxxx xx 588 Mar 8 17:09 TRACE.2022-03-08-17.0098.log
-rw-r--r-- 1 xxxxxxx xx 213 Mar 8 17:09 TRACE.2022-03-08-17.0099.log
-rw-r--r-- 1 xxxxxxx xx 65619 Mar 8 17:26 TRACE.2022-03-08-17.0100.log
At 100, log4j simply stops cycling.
The docs don't mention any such limit. What gives?
Below is my latest configuration. The app is a minimal example, rolls the file at just 500 bytes and is intended to restart cycles on hour boundaries rather than days, but the principles should still apply.
The config, also, is one sample of many variations on combinations of trigger and rollover strategy, filename pattern and appender. Everything I've tried has run into this limit.
I can think of some workarounds but I'd prefer to understand the cause of the problem better. I'd appreciate any insight or advice I can get.
1 <?xml version="1.0" encoding="UTF-8"?>
2 <Configuration status="WARN">
3 <Appenders>
4 <RollingFile name="TRACE"
5 filePattern="./trace/TRACE.%d{YYYY-MM-dd-HH}.%04i.log">
6 <!-- ex-fileName= "./trace/TRACE.log" -->
7 <Policies>
8 <OnStartupTriggeringPolicy />
9 <SizeBasedTriggeringPolicy size="500 B" />
10 <TimeBasedTriggeringPolicy />
11 <!-- CronTriggeringPolicy schedule="0 0 * * * ?" / -->
12 </Policies>
13 <DirectWriteRolloverStrategy />
14 <PatternLayout>
15 <header>%d{ISO8601} ---------- Trace file started. ----------\n</header>
16 <footer>%d{ISO8601} ----------- Trace file ended. -----------\n</footer>
17 <Pattern>%d{ISO8601} [%-4.-4t %-15.-15M#%4L] %-5level %msg%n%xEx{full}</Pattern>
18 </PatternLayout> "/>
19 </RollingFile>
20 </Appenders>
21 <Loggers>
22 <Root level="error">
23 <AppenderRef ref="TRACE"/>
24 </Root>
25 <Logger name="minimal" level="trace" additivity="false">
26 <AppenderRef ref="TRACE"/>
27 </Logger>
28 </Loggers>
29 </Configuration>
%04i
prevents rollover. Try to add inside<RollingFile>
the entry<DefaultRolloverStrategy max="200"/>
. If it helps, choose a big maximum. See Appenders for examples of usingDefaultRolloverStrategy max
.