This is an automated email from the ASF dual-hosted git repository.
pkarwasz pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/2.x by this push:
new 4a7ddf1dfb Prevent NPE with rootLogger shorthand when no appenders are
specified (#3650)
4a7ddf1dfb is described below
commit 4a7ddf1dfb2c5bd8267fd2bd35ea5ccc8fd879ad
Author: jhl221123 <[email protected]>
AuthorDate: Thu May 8 23:49:04 2025 +0900
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
---
.../logging/log4j/core/config/LoggerConfigTest.java | 17 +++++++++++++++++
.../apache/logging/log4j/core/config/LoggerConfig.java | 4 ++--
.../.2.x.x/3206_fix_rootLogger_shorthand_npe.xml | 10 ++++++++++
3 files changed, 29 insertions(+), 2 deletions(-)
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 5098c34ccb..a1672d155b 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 java.util.concurrent.atomic.AtomicReference;
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 @@ class LoggerConfigTest {
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 797b12be89..309e4c78c5 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 @@ public class LoggerConfig extends AbstractFilterable
implements LocationAware {
}
final String[] parts = Strings.splitList(levelAndRefs);
result.level = Level.getLevel(parts[0]);
+ final List<AppenderRef> refList = new ArrayList<>();
if (parts.length > 1) {
- final List<AppenderRef> 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 0000000000..e22e2d3e34
--- /dev/null
+++ b/src/changelog/.2.x.x/3206_fix_rootLogger_shorthand_npe.xml
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns="https://logging.apache.org/xml/ns"
+ xsi:schemaLocation="https://logging.apache.org/xml/ns
https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
+ type="fixed">
+ <issue id="3206"
link="https://github.com/apache/logging-log4j2/issues/3206"/>
+ <description format="asciidoc">
+ Fix NullPointerException when using `rootLogger = LEVEL` shorthand in
properties without appender.
+ </description>
+</entry>
\ No newline at end of file