0z5a opened a new pull request, #4485:
URL: https://github.com/apache/logging-log4j2/pull/4485
`PatternProcessor.calculateFrequency()` derives the rollover frequency by
looking for pattern letters (`S`, `s`, `m`, `H`/`K`/`h`/`k`, `d`, `w`, `M`,
`y`, ...) anywhere in the `%d{...}` pattern of `filePattern`. It does not skip
quoted literals, so letters that are only part of a literal text are treated as
date fields.
The manual says that `TimeBasedTriggeringPolicy` "causes a rollover, when
the smallest time unit in `filePattern` changes value". With a quoted literal
the computed unit is much smaller than the real one:
| `filePattern` date part | Expected frequency | Actual frequency |
|---|---|---|
| `%d{yyyy-MM-dd'_logs'}` | `DAILY` | `EVERY_SECOND` (`s` in `_logs`) |
| `%d{yyyy-MM-dd'_o''clock'}` | `DAILY` | `HOURLY` (`k` in `clock`) |
| `%d{yyyy-'week'ww}` | `WEEKLY` | `HOURLY` (`k` in `week`) |
| `%d{yyyy-MM'm'}` | `MONTHLY` | `EVERY_MINUTE` |
This loses log events. With `filePattern="app-%d{yyyy-MM-dd'_logs'}.log"`
and a plain `<TimeBasedTriggeringPolicy/>`, the appender rolls over every
second, but the target name only changes once a day, so each rollover replaces
the previously archived file. Logging one line per second for four seconds
leaves only 2 of the 4 lines on disk. The same configuration with
`%d{yyyy-MM-dd}` keeps all 4 lines, and with this change the quoted variant
keeps all 4 lines as well.
The fix removes quoted literals (including escaped apostrophes, `''`) from
the date pattern before looking for the pattern letters. Patterns without
quotes are not affected. Both the `DateTimeFormatter` patterns and the legacy
formatter patterns use the same quoting rules, so the change applies to both.
Tests: added a parameterized `PatternProcessorTest` case for the frequencies
above (plus `yyyy-MM-dd` and the existing `yyyy-MM-'W'W` as controls) and a
`getNextTime()` case for the daily quoted pattern. The new cases fail without
the fix (5 failures) and pass with it. `PatternProcessorTest`,
`RollingAppenderTime*Test`, `RollingAppenderSizeWithTimeTest`,
`RollingAppenderCronTest`, `RollingFileAppenderBuilderTest` and the
`DatePatternConverter*Test` classes pass with the change.
## Checklist
* Base your changes on `2.x` branch if you are targeting Log4j 2; use `main`
otherwise: based on `2.x`
* `./mvnw verify` succeeds: only the targeted tests listed above were run
* Non-trivial changes contain an entry file in the `src/changelog/.2.x.x`
directory: `fix_rollover_frequency_quoted_literals.xml`
* Tests are provided: yes, in `PatternProcessorTest`
This change was drafted with Claude Code (claude-opus-5-5); the test results
above are from the runs described.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]