Skip to content

Commit 4a7ddf1

Browse files
authored
Prevent NPE with rootLogger shorthand when no appenders are specified (#3650)
* Fix NPE with rootLogger shorthand when no appenders are specified * Add changelog entry for #3206
1 parent 3709962 commit 4a7ddf1

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/LoggerConfigTest.java

+17
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
package org.apache.logging.log4j.core.config;
1818

1919
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertNotNull;
2021
import static org.junit.jupiter.api.Assertions.assertNotSame;
2122
import static org.junit.jupiter.api.Assertions.assertNull;
2223
import static org.junit.jupiter.api.Assertions.assertSame;
24+
import static org.junit.jupiter.api.Assertions.assertTrue;
2325
import static org.mockito.ArgumentMatchers.any;
2426
import static org.mockito.Mockito.mock;
2527
import static org.mockito.Mockito.times;
@@ -32,6 +34,7 @@
3234
import org.apache.logging.log4j.Level;
3335
import org.apache.logging.log4j.core.Appender;
3436
import org.apache.logging.log4j.core.Filter;
37+
import org.apache.logging.log4j.core.config.properties.PropertiesConfiguration;
3538
import org.apache.logging.log4j.core.impl.Log4jLogEvent.Builder;
3639
import org.apache.logging.log4j.message.SimpleMessage;
3740
import org.junit.jupiter.api.Test;
@@ -137,4 +140,18 @@ void testSingleFilterInvocation() {
137140
verify(appender, times(1)).append(any());
138141
verify(filter, times(1)).filter(any());
139142
}
143+
144+
@Test
145+
void testLevelAndRefsWithoutAppenderRef() {
146+
final Configuration configuration = mock(PropertiesConfiguration.class);
147+
final LoggerConfig.Builder builder = LoggerConfig.newBuilder()
148+
.withLoggerName(FQCN)
149+
.withConfig(configuration)
150+
.withLevelAndRefs(Level.INFO.name());
151+
152+
final LoggerConfig loggerConfig = builder.build();
153+
154+
assertNotNull(loggerConfig.getAppenderRefs());
155+
assertTrue(loggerConfig.getAppenderRefs().isEmpty());
156+
}
140157
}

log4j-core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1009,13 +1009,13 @@ protected static LevelAndRefs getLevelAndRefs(
10091009
}
10101010
final String[] parts = Strings.splitList(levelAndRefs);
10111011
result.level = Level.getLevel(parts[0]);
1012+
final List<AppenderRef> refList = new ArrayList<>();
10121013
if (parts.length > 1) {
1013-
final List<AppenderRef> refList = new ArrayList<>();
10141014
Arrays.stream(parts)
10151015
.skip(1)
10161016
.forEach((ref) -> refList.add(AppenderRef.createAppenderRef(ref, null, null)));
1017-
result.refs = refList;
10181017
}
1018+
result.refs = refList;
10191019
} else {
10201020
LOGGER.warn("levelAndRefs are only allowed in a properties configuration. The value is ignored.");
10211021
result.level = level;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="https://logging.apache.org/xml/ns"
4+
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
5+
type="fixed">
6+
<issue id="3206" link="https://github.com/apache/logging-log4j2/issues/3206"/>
7+
<description format="asciidoc">
8+
Fix NullPointerException when using `rootLogger = LEVEL` shorthand in properties without appender.
9+
</description>
10+
</entry>

0 commit comments

Comments
 (0)