You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by gg...@apache.org on 2022/02/21 16:35:37 UTC

[logging-log4j2] branch release-2.x updated: LOG4J2-3407 Check for non-existent appender when parsing properties (#761)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/release-2.x by this push:
     new 14fa165  LOG4J2-3407 Check for non-existent appender when parsing properties (#761)
14fa165 is described below

commit 14fa165935faca61f5dc168a70925c87555e4cca
Author: Kenny MacLeod <km...@atlassian.com>
AuthorDate: Mon Feb 21 16:35:31 2022 +0000

    LOG4J2-3407 Check for non-existent appender when parsing properties (#761)
---
 .../log4j/config/PropertiesConfiguration.java      |  6 ++++-
 .../log4j/config/PropertiesConfigurationTest.java  | 11 ++++++++-
 .../src/test/resources/LOG4J2-3407.properties      | 26 ++++++++++++++++++++++
 3 files changed, 41 insertions(+), 2 deletions(-)

diff --git a/log4j-1.2-api/src/main/java/org/apache/log4j/config/PropertiesConfiguration.java b/log4j-1.2-api/src/main/java/org/apache/log4j/config/PropertiesConfiguration.java
index 0a0cb45..a92949e 100644
--- a/log4j-1.2-api/src/main/java/org/apache/log4j/config/PropertiesConfiguration.java
+++ b/log4j-1.2-api/src/main/java/org/apache/log4j/config/PropertiesConfiguration.java
@@ -437,6 +437,10 @@ public class PropertiesConfiguration extends Log4j1Configuration {
         final String layoutPrefix = prefix + ".layout";
         final String filterPrefix = APPENDER_PREFIX + appenderName + ".filter.";
         final String className = OptionConverter.findAndSubst(prefix, props);
+        if (className == null) {
+            LOGGER.debug("Appender \"" + appenderName + "\" does not exist.");
+            return null;
+        }
         appender = manager.parseAppender(appenderName, className, prefix, layoutPrefix, filterPrefix, props, this);
         if (appender == null) {
             appender = buildAppender(appenderName, className, prefix, layoutPrefix, filterPrefix, props);
@@ -506,7 +510,7 @@ public class PropertiesConfiguration extends Log4j1Configuration {
         final ErrorHandler eh = newInstanceOf(errorHandlerClass, "ErrorHandler");
         final String[] keys = new String[] {
             // @formatter:off
-            errorHandlerPrefix + "." + ROOT_REF, 
+            errorHandlerPrefix + "." + ROOT_REF,
             errorHandlerPrefix + "." + LOGGER_REF,
             errorHandlerPrefix + "." + APPENDER_REF_TAG};
             // @formatter:on
diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/config/PropertiesConfigurationTest.java b/log4j-1.2-api/src/test/java/org/apache/log4j/config/PropertiesConfigurationTest.java
index ce27715..64a2d5b 100644
--- a/log4j-1.2-api/src/test/java/org/apache/log4j/config/PropertiesConfigurationTest.java
+++ b/log4j-1.2-api/src/test/java/org/apache/log4j/config/PropertiesConfigurationTest.java
@@ -113,7 +113,7 @@ public class PropertiesConfigurationTest extends AbstractLog4j1ConfigurationTest
             assertTrue(filter.getFilter() instanceof NeutralFilterFixture);
         }
     }
-    
+
     @Test
     public void testConsoleAppenderLevelRangeFilter() throws Exception {
         PluginManager.addPackage("org.apache.log4j.builders.filter");
@@ -133,6 +133,15 @@ public class PropertiesConfigurationTest extends AbstractLog4j1ConfigurationTest
     }
 
     @Test
+    public void testConfigureAppenderDoesNotExist() throws Exception {
+        // Verify that we tolerate a logger which specifies an appender that does not exist.
+        try (LoggerContext loggerContext = TestConfigurator.configure("target/test-classes/LOG4J2-3407.properties")) {
+            final Configuration configuration = loggerContext.getConfiguration();
+            assertNotNull(configuration);
+        }
+    }
+
+    @Test
     public void testListAppender() throws Exception {
         try (LoggerContext loggerContext = TestConfigurator.configure("target/test-classes/log4j1-list.properties")) {
             final Logger logger = LogManager.getLogger("test");
diff --git a/log4j-1.2-api/src/test/resources/LOG4J2-3407.properties b/log4j-1.2-api/src/test/resources/LOG4J2-3407.properties
new file mode 100644
index 0000000..499a458
--- /dev/null
+++ b/log4j-1.2-api/src/test/resources/LOG4J2-3407.properties
@@ -0,0 +1,26 @@
+#
+# 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.
+#
+
+log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
+log4j.appender.CONSOLE.filter.1=org.apache.log4j.config.NeutralFilterFixture
+log4j.appender.CONSOLE.filter.1.onMatch=neutral
+log4j.appender.CONSOLE.Target=System.out
+log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
+log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
+
+log4j.logger.org.apache.log4j.xml=trace, A1
+log4j.rootLogger=trace, CONSOLE