Skip to content

Commit 054a4f7

Browse files
committed
Fix NPE in PatternProcessor for the UNIX pattern
Closes 2346. The constructor of `PatternProcessor` fails if the date pattern is `UNIX` and `UNIX_MILLIS`: `DatePatterConverter#getPattern()` returns intentionally `null` in those cases.
1 parent 0eb232f commit 054a4f7

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

Diff for: log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessorTest.java

+9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.logging.log4j.core.appender.rolling;
1818

19+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
1920
import static org.junit.jupiter.api.Assertions.assertEquals;
2021

2122
import java.time.Instant;
@@ -29,6 +30,8 @@
2930
import org.junit.jupiter.api.parallel.ResourceAccessMode;
3031
import org.junit.jupiter.api.parallel.ResourceLock;
3132
import org.junit.jupiter.api.parallel.Resources;
33+
import org.junit.jupiter.params.ParameterizedTest;
34+
import org.junit.jupiter.params.provider.ValueSource;
3235

3336
/**
3437
* Tests the PatternProcessor class.
@@ -394,4 +397,10 @@ public void testGetNextTimeDailyReturnsFirstHourOfNextDayInGmtIfZoneIsInvalid()
394397
TimeZone.setDefault(old);
395398
}
396399
}
400+
401+
@ParameterizedTest
402+
@ValueSource(strings = {"%d{UNIX}", "%d{UNIX_MILLIS}"})
403+
void does_not_throw_with_unix_pattern(final String pattern) {
404+
assertDoesNotThrow(() -> new PatternProcessor(pattern));
405+
}
397406
}

Diff for: log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessor.java

+4
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,10 @@ protected final void formatFileName(final StringBuilder buf, final Object... obj
325325
}
326326

327327
private RolloverFrequency calculateFrequency(final String pattern) {
328+
// The UNIX and UNIX_MILLIS converters do not have a pattern
329+
if (pattern == null) {
330+
return null;
331+
}
328332
if (patternContains(pattern, MILLIS_CHAR)) {
329333
return RolloverFrequency.EVERY_MILLISECOND;
330334
}

Diff for: log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/DatePatternConverter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ public void format(final StringBuilder toAppendTo, final Object... objects) {
359359
/**
360360
* Gets the pattern string describing this date format.
361361
*
362-
* @return the pattern string describing this date format.
362+
* @return the pattern string describing this date format or {@code null} if the format does not have a pattern.
363363
*/
364364
public String getPattern() {
365365
return formatter.toPattern();
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://logging.apache.org/log4j/changelog"
4+
xsi:schemaLocation="http://logging.apache.org/log4j/changelog https://logging.apache.org/log4j/changelog-0.1.3.xsd"
5+
type="changed">
6+
<issue id="2346" link="https://github.com/apache/logging-log4j2/pull/2346"/>
7+
<description format="asciidoc">Fix `NullPointerException` in `PatternProcessor` for a `UNIX_MILLIS` pattern.</description>
8+
</entry>

0 commit comments

Comments
 (0)