LOG4J2-1153: Fixed NullPointerException when only root logger is defined
(and no named loggers) in configuration properties file.
- bugfix in PropertiesConfigurationFactory
- removed unnecessary @SuppressWarnings annotation
- added JUnit test demonstrating the issue and proving that the fix
works
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/properties/PropertiesConfigurationFactory.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/properties/PropertiesConfigurationFactory.java
index 50f9285..27644d8 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/properties/PropertiesConfigurationFactory.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/properties/PropertiesConfigurationFactory.java
@@ -130,7 +130,7 @@
}
}
String loggerProp = properties.getProperty("loggers");
- if (appenderProp != null) {
+ if (loggerProp != null) {
String[] loggerNames = loggerProp.split(",");
for (String loggerName : loggerNames) {
String name = loggerName.trim();
@@ -343,7 +343,6 @@
return componentBuilder;
}
- @SuppressWarnings({"unchecked", "rawtypes"})
private void processRemainingProperties(ComponentBuilder<?> builder, String name, Properties properties) {
while (properties.size() > 0) {
String propertyName = properties.stringPropertyNames().iterator().next();
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/properties/PropertiesConfigurationRootLoggerOnlyTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/properties/PropertiesConfigurationRootLoggerOnlyTest.java
new file mode 100644
index 0000000..88b0709
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/properties/PropertiesConfigurationRootLoggerOnlyTest.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.core.config.properties;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.Appender;
+import org.apache.logging.log4j.core.Filter;
+import org.apache.logging.log4j.core.LifeCycle;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.config.ConfigurationFactory;
+import org.apache.logging.log4j.core.config.LoggerConfig;
+import org.apache.logging.log4j.core.filter.ThresholdFilter;
+import org.junit.Test;
+
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+/**
+ *
+ */
+public class PropertiesConfigurationRootLoggerOnlyTest {
+
+ @Test
+ public void testPropertiesConfiguration() {
+ System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "target/test-classes/log4j2-properties-root-only.properties");
+ Configuration config = ((LoggerContext)LogManager.getContext(false)).getConfiguration();
+ assertNotNull("No configuration created", config);
+ assertEquals("Incorrect State: " + config.getState(), config.getState(), LifeCycle.State.STARTED);
+ Map<String, Appender> appenders = config.getAppenders();
+ assertNotNull(appenders);
+ assertTrue("Incorrect number of Appenders: " + appenders.size(), appenders.size() == 1);
+ Map<String, LoggerConfig> loggers = config.getLoggers();
+ assertNotNull(loggers);
+ assertTrue("Incorrect number of LoggerConfigs: " + loggers.size(), loggers.size() == 1);
+ Filter filter = config.getFilter();
+ assertNotNull("No Filter", filter);
+ assertTrue("Not a Threshold Filter", filter instanceof ThresholdFilter);
+ Logger logger = LogManager.getLogger(getClass());
+ logger.info("Welcome to Log4j!");
+ }
+}
diff --git a/log4j-core/src/test/resources/log4j2-properties-root-only.properties b/log4j-core/src/test/resources/log4j2-properties-root-only.properties
new file mode 100644
index 0000000..7587606
--- /dev/null
+++ b/log4j-core/src/test/resources/log4j2-properties-root-only.properties
@@ -0,0 +1,38 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+status = ERROR
+
+filters = Threshold
+
+filter.Threshold.type = ThresholdFilter
+filter.Threshold.level = DEBUG
+
+appenders = Stdout
+
+appender.Stdout.type = Console
+appender.Stdout.name = StdOut
+appender.Stdout.target = SYSTEM_OUT
+appender.Stdout.layout.type = PatternLayout
+appender.Stdout.layout.pattern = %d [%t] %-5level: %msg%n%throwable
+appender.Stdout.filters = marker
+appender.Stdout.filter.marker.type = MarkerFilter
+appender.Stdout.filter.marker.onMatch = DENY
+appender.Stdout.filter.marker.onMisMatch = NEUTRAL
+appender.Stdout.filter.marker.marker = FLOW
+
+rootLogger.appenderRefs = console
+rootLogger.appenderRef.console.ref = StdOut
+rootLogger.level = ERROR
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index f7f5df1..ab6a089 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -39,6 +39,9 @@
<action issue="LOG4J2-1020" dev="mikes" type="add">
Add possibility to set shutdown timeout on AsyncAppender.
</action>
+ <action issue="LOG4J2-1153" dev="rpopma" type="fix">
+ Fixed NullPointerException when only root logger is defined (and no named loggers) in configuration properties file.
+ </action>
<action issue="LOG4J2-1140" dev="rpopma" type="fix">
Fixed bug where headers were not being written to first file with RollingFileAppender.
</action>