Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NPE in PatternProcessor for the UNIX pattern #2350

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.logging.log4j.core.appender.rolling;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.time.Instant;
Expand All @@ -29,6 +30,8 @@
import org.junit.jupiter.api.parallel.ResourceAccessMode;
import org.junit.jupiter.api.parallel.ResourceLock;
import org.junit.jupiter.api.parallel.Resources;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

/**
* Tests the PatternProcessor class.
Expand Down Expand Up @@ -394,4 +397,10 @@ public void testGetNextTimeDailyReturnsFirstHourOfNextDayInGmtIfZoneIsInvalid()
TimeZone.setDefault(old);
}
}

@ParameterizedTest
@ValueSource(strings = {"%d{UNIX}", "%d{UNIX_MILLIS}"})
void does_not_throw_with_unix_pattern(final String pattern) {
assertDoesNotThrow(() -> new PatternProcessor(pattern));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ protected final void formatFileName(final StringBuilder buf, final Object... obj
}

private RolloverFrequency calculateFrequency(final String pattern) {
// The UNIX and UNIX_MILLIS converters do not have a pattern
if (pattern == null) {
return null;
}
if (patternContains(pattern, MILLIS_CHAR)) {
return RolloverFrequency.EVERY_MILLISECOND;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ public void format(final StringBuilder toAppendTo, final Object... objects) {
/**
* Gets the pattern string describing this date format.
*
* @return the pattern string describing this date format.
* @return the pattern string describing this date format or {@code null} if the format does not have a pattern.
*/
public String getPattern() {
return formatter.toPattern();
Expand Down
8 changes: 8 additions & 0 deletions src/changelog/.2.x.x/2346_unix_millis_data_pattern.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://logging.apache.org/log4j/changelog"
xsi:schemaLocation="http://logging.apache.org/log4j/changelog https://logging.apache.org/log4j/changelog-0.1.3.xsd"
type="changed">
<issue id="2346" link="https:/apache/logging-log4j2/pull/2346"/>
<description format="asciidoc">Fix `NullPointerException` in `PatternProcessor` for a `UNIX_MILLIS` pattern.</description>
</entry>
Loading