diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/LoggerConfigTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/LoggerConfigTest.java index 5098c34ccb3..a1672d155b0 100644 --- a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/LoggerConfigTest.java +++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/LoggerConfigTest.java @@ -17,9 +17,11 @@ package org.apache.logging.log4j.core.config; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; @@ -32,6 +34,7 @@ import org.apache.logging.log4j.Level; import org.apache.logging.log4j.core.Appender; import org.apache.logging.log4j.core.Filter; +import org.apache.logging.log4j.core.config.properties.PropertiesConfiguration; import org.apache.logging.log4j.core.impl.Log4jLogEvent.Builder; import org.apache.logging.log4j.message.SimpleMessage; import org.junit.jupiter.api.Test; @@ -137,4 +140,18 @@ void testSingleFilterInvocation() { verify(appender, times(1)).append(any()); verify(filter, times(1)).filter(any()); } + + @Test + void testLevelAndRefsWithoutAppenderRef() { + final Configuration configuration = mock(PropertiesConfiguration.class); + final LoggerConfig.Builder builder = LoggerConfig.newBuilder() + .withLoggerName(FQCN) + .withConfig(configuration) + .withLevelAndRefs(Level.INFO.name()); + + final LoggerConfig loggerConfig = builder.build(); + + assertNotNull(loggerConfig.getAppenderRefs()); + assertTrue(loggerConfig.getAppenderRefs().isEmpty()); + } } diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java index 797b12be896..309e4c78c51 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java @@ -1009,13 +1009,13 @@ protected static LevelAndRefs getLevelAndRefs( } final String[] parts = Strings.splitList(levelAndRefs); result.level = Level.getLevel(parts[0]); + final List refList = new ArrayList<>(); if (parts.length > 1) { - final List refList = new ArrayList<>(); Arrays.stream(parts) .skip(1) .forEach((ref) -> refList.add(AppenderRef.createAppenderRef(ref, null, null))); - result.refs = refList; } + result.refs = refList; } else { LOGGER.warn("levelAndRefs are only allowed in a properties configuration. The value is ignored."); result.level = level; diff --git a/src/changelog/.2.x.x/3206_fix_rootLogger_shorthand_npe.xml b/src/changelog/.2.x.x/3206_fix_rootLogger_shorthand_npe.xml new file mode 100644 index 00000000000..e22e2d3e340 --- /dev/null +++ b/src/changelog/.2.x.x/3206_fix_rootLogger_shorthand_npe.xml @@ -0,0 +1,10 @@ + + + + + Fix NullPointerException when using `rootLogger = LEVEL` shorthand in properties without appender. + + \ No newline at end of file