Skip to content

Commit 48ebcf9

Browse files
committed
Fix NPE in PatternProcessor for the UNIX pattern
Closes 2346. The constructor of `PatternProcessor` fails to recognize the `UNIX` and `UNIX_MILLIS` data patterns and deduce the correct rollover frequency. This PR maps those two patterns to a rollover frequency of second and millisecond respectively.
1 parent eb8bc2f commit 48ebcf9

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package org.apache.logging.log4j.core.appender.rolling;
1818

19+
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
1921
import static org.junit.jupiter.api.Assertions.assertEquals;
2022

2123
import java.time.Instant;
@@ -25,10 +27,14 @@
2527
import java.util.Calendar;
2628
import java.util.Locale;
2729
import java.util.TimeZone;
30+
import java.util.stream.Stream;
2831
import org.junit.jupiter.api.Test;
2932
import org.junit.jupiter.api.parallel.ResourceAccessMode;
3033
import org.junit.jupiter.api.parallel.ResourceLock;
3134
import org.junit.jupiter.api.parallel.Resources;
35+
import org.junit.jupiter.params.ParameterizedTest;
36+
import org.junit.jupiter.params.provider.Arguments;
37+
import org.junit.jupiter.params.provider.MethodSource;
3238

3339
/**
3440
* Tests the PatternProcessor class.
@@ -394,4 +400,17 @@ public void testGetNextTimeDailyReturnsFirstHourOfNextDayInGmtIfZoneIsInvalid()
394400
TimeZone.setDefault(old);
395401
}
396402
}
403+
404+
static Stream<Arguments> works_with_unix_pattern() {
405+
return Stream.of(
406+
Arguments.of("%d{UNIX}", RolloverFrequency.EVERY_SECOND),
407+
Arguments.of("%d{UNIX_MILLIS}", RolloverFrequency.EVERY_MILLISECOND));
408+
}
409+
410+
@ParameterizedTest
411+
@MethodSource
412+
void works_with_unix_pattern(final String pattern, final RolloverFrequency expectedFrequency) {
413+
final PatternProcessor processor = assertDoesNotThrow(() -> new PatternProcessor(pattern));
414+
assertThat(processor.getFrequency()).isEqualTo(expectedFrequency);
415+
}
397416
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,12 @@ protected final void formatFileName(final StringBuilder buf, final Object... obj
325325
}
326326

327327
private RolloverFrequency calculateFrequency(final String pattern) {
328+
if ("UNIX".equals(pattern)) {
329+
return RolloverFrequency.EVERY_SECOND;
330+
}
331+
if ("UNIX_MILLIS".equals(pattern)) {
332+
return RolloverFrequency.EVERY_MILLISECOND;
333+
}
328334
if (patternContains(pattern, MILLIS_CHAR)) {
329335
return RolloverFrequency.EVERY_MILLISECOND;
330336
}

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ private abstract static class Formatter {
4848

4949
abstract void formatToBuffer(final Instant instant, StringBuilder destination);
5050

51-
public String toPattern() {
52-
return null;
53-
}
51+
public abstract String toPattern();
5452

5553
public TimeZone getTimeZone() {
5654
return TimeZone.getDefault();
@@ -143,6 +141,11 @@ String format(final Instant instant) {
143141
void formatToBuffer(final Instant instant, final StringBuilder destination) {
144142
destination.append(instant.getEpochSecond()); // no need for caching
145143
}
144+
145+
@Override
146+
public String toPattern() {
147+
return UNIX_FORMAT;
148+
}
146149
}
147150

148151
private static final class UnixMillisFormatter extends Formatter {
@@ -156,6 +159,11 @@ String format(final Instant instant) {
156159
void formatToBuffer(final Instant instant, final StringBuilder destination) {
157160
destination.append(instant.getEpochMillisecond()); // no need for caching
158161
}
162+
163+
@Override
164+
public String toPattern() {
165+
return UNIX_MILLIS_FORMAT;
166+
}
159167
}
160168

161169
private final class CachedTime {
Lines changed: 8 additions & 0 deletions
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)