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/01/04 12:22:31 UTC

[logging-log4j2] branch release-2.x updated: [LOG4J2-3281] PropertiesConfiguration.buildAppender not adding filters to custom appender.

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 79d6427  [LOG4J2-3281] PropertiesConfiguration.buildAppender not adding filters to custom appender.
79d6427 is described below

commit 79d64279a022040518c3dd2f0472326af1964343
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Jan 4 07:22:27 2022 -0500

    [LOG4J2-3281] PropertiesConfiguration.buildAppender not adding filters
    to custom appender.
    
    Oops, had reverted the wrong commit.
---
 .../org/apache/log4j/builders/BuilderManager.java  | 13 +++++---
 .../log4j/config/PropertiesConfiguration.java      |  6 ++--
 .../java/org/apache/log4j/CustomNoopAppender.java  | 39 ++++++++++++++++++++++
 .../log4j/config/PropertiesConfigurationTest.java  | 17 +++++++++-
 .../src/test/resources/LOG4J2-3281.properties      | 25 ++++++++++++++
 5 files changed, 90 insertions(+), 10 deletions(-)

diff --git a/log4j-1.2-api/src/main/java/org/apache/log4j/builders/BuilderManager.java b/log4j-1.2-api/src/main/java/org/apache/log4j/builders/BuilderManager.java
index 6e9b240..1bbbe9c 100644
--- a/log4j-1.2-api/src/main/java/org/apache/log4j/builders/BuilderManager.java
+++ b/log4j-1.2-api/src/main/java/org/apache/log4j/builders/BuilderManager.java
@@ -36,6 +36,7 @@ import org.w3c.dom.Element;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 import java.util.Map;
+import java.util.Objects;
 import java.util.Properties;
 
 /**
@@ -60,7 +61,7 @@ public class BuilderManager {
             try {
                 AppenderBuilder builder = (AppenderBuilder) LoaderUtil.newInstanceOf(plugin.getPluginClass());
                 return builder.parseAppender(appenderElement, config);
-            } catch (InstantiationException | IllegalAccessException | InvocationTargetException ex) {
+            } catch (ReflectiveOperationException ex) {
                 LOGGER.warn("Unable to load plugin: {} due to: {}", plugin.getKey(), ex.getMessage());
             }
         }
@@ -69,6 +70,8 @@ public class BuilderManager {
 
     public Appender parseAppender(String name, String className, String prefix, String layoutPrefix,
             String filterPrefix, Properties props, PropertiesConfiguration config) {
+        Objects.requireNonNull(plugins, "plugins");
+        Objects.requireNonNull(className, "className");
         PluginType<?> plugin = plugins.get(className.toLowerCase());
         if (plugin != null) {
             AppenderBuilder builder = createBuilder(plugin, prefix, props);
@@ -85,7 +88,7 @@ public class BuilderManager {
             try {
                 FilterBuilder builder = (FilterBuilder) LoaderUtil.newInstanceOf(plugin.getPluginClass());
                 return builder.parseFilter(filterElement, config);
-            } catch (InstantiationException | IllegalAccessException | InvocationTargetException ex) {
+            } catch (ReflectiveOperationException ex) {
                 LOGGER.warn("Unable to load plugin: {} due to: {}", plugin.getKey(), ex.getMessage());
             }
         }
@@ -109,7 +112,7 @@ public class BuilderManager {
             try {
                 LayoutBuilder builder = (LayoutBuilder) LoaderUtil.newInstanceOf(plugin.getPluginClass());
                 return builder.parseLayout(layoutElement, config);
-            } catch (InstantiationException | IllegalAccessException | InvocationTargetException ex) {
+            } catch (ReflectiveOperationException ex) {
                 LOGGER.warn("Unable to load plugin: {} due to: {}", plugin.getKey(), ex.getMessage());
             }
         }
@@ -132,7 +135,7 @@ public class BuilderManager {
             try {
                 RewritePolicyBuilder builder = (RewritePolicyBuilder) LoaderUtil.newInstanceOf(plugin.getPluginClass());
                 return builder.parseRewritePolicy(rewriteElement, config);
-            } catch (InstantiationException | IllegalAccessException | InvocationTargetException ex) {
+            } catch (ReflectiveOperationException ex) {
                 LOGGER.warn("Unable to load plugin: {} due to: {}", plugin.getKey(), ex.getMessage());
             }
         }
@@ -161,7 +164,7 @@ public class BuilderManager {
             @SuppressWarnings("unchecked")
             T builder = (T) LoaderUtil.newInstanceOf(clazz);
             return builder;
-        } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException ex) {
+        } catch (ReflectiveOperationException ex) {
             LOGGER.warn("Unable to load plugin: {} due to: {}", plugin.getKey(), ex.getMessage());
             return null;
         }
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 8b9b3b9..1ca70d4 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
@@ -455,10 +455,8 @@ public class PropertiesConfiguration  extends Log4j1Configuration {
                 appender.setErrorHandler(eh);
             }
         }
-        parseAppenderFilters(props, filterPrefix, appenderName);
-        String[] keys = new String[] {
-                layoutPrefix,
-        };
+        appender.addFilter(parseAppenderFilters(props, filterPrefix, appenderName));
+        String[] keys = new String[] { layoutPrefix };
         addProperties(appender, keys, props, prefix);
         if (appender instanceof AppenderWrapper) {
             addAppender(((AppenderWrapper) appender).getAppender());
diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/CustomNoopAppender.java b/log4j-1.2-api/src/test/java/org/apache/log4j/CustomNoopAppender.java
new file mode 100644
index 0000000..49de446
--- /dev/null
+++ b/log4j-1.2-api/src/test/java/org/apache/log4j/CustomNoopAppender.java
@@ -0,0 +1,39 @@
+/*
+ * 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.log4j;
+
+import org.apache.log4j.spi.LoggingEvent;
+
+public class CustomNoopAppender extends AppenderSkeleton {
+
+    @Override
+    public void close() {
+        // Noop
+    }
+
+    @Override
+    public boolean requiresLayout() {
+        return false;
+    }
+
+    @Override
+    protected void append(LoggingEvent event) {
+        // Noop
+    }
+
+}
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 64ee9e3..59757cf 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
@@ -52,7 +52,7 @@ public class PropertiesConfigurationTest {
     }
 
     @Test
-    public void testFilter() throws Exception {
+    public void testConsoleAppenderFilter() throws Exception {
         try (LoggerContext loggerContext = TestConfigurator.configure("target/test-classes/LOG4J2-3247.properties")) {
             // LOG4J2-3281 PropertiesConfiguration.buildAppender not adding filters to appender
             Configuration configuration = loggerContext.getConfiguration();
@@ -67,6 +67,21 @@ public class PropertiesConfigurationTest {
     }
 
     @Test
+    public void testCustomAppenderFilter() throws Exception {
+        try (LoggerContext loggerContext = TestConfigurator.configure("target/test-classes/LOG4J2-3281.properties")) {
+            // LOG4J2-3281 PropertiesConfiguration.buildAppender not adding filters to appender
+            Configuration configuration = loggerContext.getConfiguration();
+            assertNotNull(configuration);
+            Appender appender = configuration.getAppender("CUSTOM");
+            assertNotNull(appender);
+            Filterable filterable = (Filterable) appender;
+            FilterAdapter filter = (FilterAdapter) filterable.getFilter();
+            assertNotNull(filter);
+            assertTrue(filter.getFilter() instanceof NeutralFilterFixture);
+        }
+    }
+
+    @Test
     public void testListAppender() throws Exception {
         try (LoggerContext loggerContext = TestConfigurator.configure("target/test-classes/log4j1-list.properties")) {
             Logger logger = LogManager.getLogger("test");
diff --git a/log4j-1.2-api/src/test/resources/LOG4J2-3281.properties b/log4j-1.2-api/src/test/resources/LOG4J2-3281.properties
new file mode 100644
index 0000000..4a45fd5
--- /dev/null
+++ b/log4j-1.2-api/src/test/resources/LOG4J2-3281.properties
@@ -0,0 +1,25 @@
+# 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.CUSTOM=org.apache.log4j.CustomNoopAppender
+log4j.appender.CUSTOM.filter.1=org.apache.log4j.config.NeutralFilterFixture
+log4j.appender.CUSTOM.filter.1.onMatch=neutral
+log4j.appender.CUSTOM.Target=System.out
+log4j.appender.CUSTOM.layout=org.apache.log4j.PatternLayout
+log4j.appender.CUSTOM.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
+
+log4j.logger.org.apache.log4j.xml=trace, CUSTOM
+log4j.rootLogger=trace, CUSTOM