Skip to content

Commit 007b983

Browse files
committed
Fix formatting of s pattern
The specialized `SecondPatternSequence` formatter is optimized to format the `ss.?S*` patterns, where seconds are padded. For the not padded (and uncommon) `s` pattern, we should use the usual `DynamicPatternSequence`.
1 parent c59fdd4 commit 007b983

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

log4j-core-test/src/test/java/org/apache/logging/log4j/core/util/internal/instant/InstantPatternDynamicFormatterTest.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ static List<Arguments> sequencingTestCases() {
100100
testCases.add(Arguments.of(
101101
"HH:mm:ss.SSSSSS", ChronoUnit.MINUTES, asList(pDyn("HH':'mm':'", ChronoUnit.MINUTES), pSec(".", 6))));
102102

103+
// Seconds without padding
104+
testCases.add(Arguments.of("s.SSS", ChronoUnit.SECONDS, asList(pDyn("s'.'", ChronoUnit.SECONDS), pMilliSec())));
105+
103106
return testCases;
104107
}
105108

@@ -352,7 +355,9 @@ static Stream<Arguments> formatterInputs() {
352355
"yyyy-MM-dd'T'HH:mm:ss.SSS",
353356
"yyyy-MM-dd'T'HH:mm:ss.SSSSSS",
354357
"dd/MM/yy HH:mm:ss.SSS",
355-
"dd/MM/yyyy HH:mm:ss.SSS")
358+
"dd/MM/yyyy HH:mm:ss.SSS",
359+
// seconds without padding
360+
"s.SSS")
356361
.flatMap(InstantPatternDynamicFormatterTest::formatterInputs);
357362
}
358363

log4j-core/src/main/java/org/apache/logging/log4j/core/util/internal/instant/InstantPatternDynamicFormatter.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,11 @@ private static List<PatternSequence> sequencePattern(final String pattern) {
239239
final PatternSequence sequence;
240240
switch (c) {
241241
case 's':
242-
sequence = new SecondPatternSequence(true, "", 0);
242+
if (sequenceContent.length() == 2) {
243+
sequence = new SecondPatternSequence(true, "", 0);
244+
} else {
245+
sequence = new DynamicPatternSequence(sequenceContent);
246+
}
243247
break;
244248
case 'S':
245249
sequence = new SecondPatternSequence(false, "", sequenceContent.length());

0 commit comments

Comments
 (0)