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 19:47:02 UTC

[logging-log4j2] branch release-2.x updated: Adds Log4j 1.x global threshold (#764)

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 00810b5  Adds Log4j 1.x global threshold (#764)
00810b5 is described below

commit 00810b50b0f373edfb06df415fd2d362de8d1058
Author: ppkarwasz <pi...@karwasz.org>
AuthorDate: Mon Feb 21 20:46:57 2022 +0100

    Adds Log4j 1.x global threshold (#764)
    
    The `log4j.threshold` configuration key was not translated into its
    Log4j 2.x equivalent global filter.
---
 .../log4j/config/Log4j1ConfigurationParser.java    | 11 ++++++++
 .../log4j/config/PropertiesConfiguration.java      | 10 +++++++
 .../org/apache/log4j/xml/XmlConfiguration.java     | 10 +++++++
 .../config/AbstractLog4j1ConfigurationTest.java    | 31 +++++++++++++++++++++-
 .../config/Log4j1ConfigurationFactoryTest.java     | 16 +++++++++++
 .../log4j/config/PropertiesConfigurationTest.java  |  6 +++++
 .../apache/log4j/config/XmlConfigurationTest.java  |  6 +++++
 .../config-1.2/log4j-global-threshold.properties   | 20 ++++++++++++++
 .../config-1.2/log4j-global-threshold.xml          | 26 ++++++++++++++++++
 9 files changed, 135 insertions(+), 1 deletion(-)

diff --git a/log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationParser.java b/log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationParser.java
index e6230aa..ca7847c 100644
--- a/log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationParser.java
+++ b/log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationParser.java
@@ -25,8 +25,10 @@ import java.util.Objects;
 import java.util.Properties;
 import java.util.TreeMap;
 
+import org.apache.log4j.LogManager;
 import org.apache.log4j.helpers.OptionConverter;
 import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.Filter.Result;
 import org.apache.logging.log4j.core.appender.ConsoleAppender;
 import org.apache.logging.log4j.core.appender.FileAppender;
 import org.apache.logging.log4j.core.appender.NullAppender;
@@ -36,10 +38,12 @@ import org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder
 import org.apache.logging.log4j.core.config.builder.api.ComponentBuilder;
 import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder;
 import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory;
+import org.apache.logging.log4j.core.config.builder.api.FilterComponentBuilder;
 import org.apache.logging.log4j.core.config.builder.api.LayoutComponentBuilder;
 import org.apache.logging.log4j.core.config.builder.api.LoggerComponentBuilder;
 import org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder;
 import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration;
+import org.apache.logging.log4j.core.filter.ThresholdFilter;
 import org.apache.logging.log4j.status.StatusLogger;
 import org.apache.logging.log4j.util.Strings;
 
@@ -103,6 +107,13 @@ public class Log4j1ConfigurationParser {
             if (Boolean.parseBoolean(debugValue)) {
                 builder.setStatusLevel(Level.DEBUG);
             }
+            // global threshold
+            final String threshold = OptionConverter.findAndSubst(PropertiesConfiguration.THRESHOLD_KEY, properties);
+            if (threshold != null) {
+                final Level level = OptionConverter.convertLevel(threshold.trim(), Level.ALL);
+                builder.add(builder.newFilter("ThresholdFilter", Result.NEUTRAL, Result.DENY)
+                        .addAttribute("level", level));
+            }
             // Root
             buildRootLogger(getLog4jValue(ROOTCATEGORY));
             buildRootLogger(getLog4jValue(ROOTLOGGER));
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 a92949e..d951fd0 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
@@ -39,11 +39,14 @@ import org.apache.log4j.bridge.FilterAdapter;
 import org.apache.log4j.helpers.OptionConverter;
 import org.apache.log4j.spi.ErrorHandler;
 import org.apache.log4j.spi.Filter;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.Filter.Result;
 import org.apache.logging.log4j.core.LoggerContext;
 import org.apache.logging.log4j.core.config.Configuration;
 import org.apache.logging.log4j.core.config.ConfigurationSource;
 import org.apache.logging.log4j.core.config.LoggerConfig;
 import org.apache.logging.log4j.core.config.status.StatusConfiguration;
+import org.apache.logging.log4j.core.filter.ThresholdFilter;
 import org.apache.logging.log4j.util.LoaderUtil;
 
 /**
@@ -66,6 +69,7 @@ public class PropertiesConfiguration extends Log4j1Configuration {
      */
     private static final String RESET_KEY = "log4j.reset";
 
+    public static final String THRESHOLD_KEY = "log4j.threshold";
     public static final String DEBUG_KEY = "log4j.debug";
 
     private static final String INTERNAL_ROOT_NAME = "root";
@@ -313,6 +317,12 @@ public class PropertiesConfiguration extends Log4j1Configuration {
             LogManager.resetConfiguration();
         }
 
+        final String threshold = OptionConverter.findAndSubst(THRESHOLD_KEY, properties);
+        if (threshold != null) {
+            final Level level = OptionConverter.convertLevel(threshold.trim(), Level.ALL);
+            addFilter(ThresholdFilter.createFilter(level, Result.NEUTRAL, Result.DENY));
+        }
+
         configureRoot(properties);
         parseLoggers(properties);
 
diff --git a/log4j-1.2-api/src/main/java/org/apache/log4j/xml/XmlConfiguration.java b/log4j-1.2-api/src/main/java/org/apache/log4j/xml/XmlConfiguration.java
index fc5ce6b..1339f57 100644
--- a/log4j-1.2-api/src/main/java/org/apache/log4j/xml/XmlConfiguration.java
+++ b/log4j-1.2-api/src/main/java/org/apache/log4j/xml/XmlConfiguration.java
@@ -44,10 +44,12 @@ import org.apache.log4j.spi.AppenderAttachable;
 import org.apache.log4j.spi.ErrorHandler;
 import org.apache.log4j.spi.Filter;
 import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.Filter.Result;
 import org.apache.logging.log4j.core.config.Configuration;
 import org.apache.logging.log4j.core.config.ConfigurationSource;
 import org.apache.logging.log4j.core.config.LoggerConfig;
 import org.apache.logging.log4j.core.config.status.StatusConfiguration;
+import org.apache.logging.log4j.core.filter.ThresholdFilter;
 import org.apache.logging.log4j.status.StatusLogger;
 import org.apache.logging.log4j.util.LoaderUtil;
 import org.w3c.dom.Document;
@@ -88,6 +90,7 @@ public class XmlConfiguration extends Log4j1Configuration {
     private static final String ADDITIVITY_ATTR = "additivity";
     private static final String CONFIG_DEBUG_ATTR = "configDebug";
     private static final String INTERNAL_DEBUG_ATTR = "debug";
+    private static final String THRESHOLD_ATTR = "threshold";
     private static final String EMPTY_STR = "";
     private static final Class<?>[] ONE_STRING_PARAM = new Class[] { String.class };
     private static final String dbfKey = "javax.xml.parsers.DocumentBuilderFactory";
@@ -742,6 +745,13 @@ public class XmlConfiguration extends Log4j1Configuration {
         final StatusConfiguration statusConfig = new StatusConfiguration().withStatus(status);
         statusConfig.initialize();
 
+        final String threshold = subst(element.getAttribute(THRESHOLD_ATTR));
+        if (threshold != null) {
+            final org.apache.logging.log4j.Level level = OptionConverter.convertLevel(threshold.trim(),
+                    org.apache.logging.log4j.Level.ALL);
+            addFilter(ThresholdFilter.createFilter(level, Result.NEUTRAL, Result.DENY));
+        }
+
         forEachElement(element.getChildNodes(), currentElement -> {
             switch (currentElement.getTagName()) {
                 case CATEGORY: 
diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/config/AbstractLog4j1ConfigurationTest.java b/log4j-1.2-api/src/test/java/org/apache/log4j/config/AbstractLog4j1ConfigurationTest.java
index 63839e2..5ddb32a 100644
--- a/log4j-1.2-api/src/test/java/org/apache/log4j/config/AbstractLog4j1ConfigurationTest.java
+++ b/log4j-1.2-api/src/test/java/org/apache/log4j/config/AbstractLog4j1ConfigurationTest.java
@@ -43,6 +43,7 @@ import org.apache.log4j.bridge.FilterAdapter;
 import org.apache.log4j.bridge.FilterWrapper;
 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.Layout;
 import org.apache.logging.log4j.core.LoggerContext;
 import org.apache.logging.log4j.core.appender.ConsoleAppender;
@@ -62,6 +63,7 @@ import org.apache.logging.log4j.core.config.Configurator;
 import org.apache.logging.log4j.core.config.LoggerConfig;
 import org.apache.logging.log4j.core.filter.CompositeFilter;
 import org.apache.logging.log4j.core.filter.Filterable;
+import org.apache.logging.log4j.core.filter.ThresholdFilter;
 import org.apache.logging.log4j.core.layout.HtmlLayout;
 import org.apache.logging.log4j.core.layout.PatternLayout;
 import org.apache.logging.log4j.core.util.CloseShieldOutputStream;
@@ -75,7 +77,7 @@ public abstract class AbstractLog4j1ConfigurationTest {
 
     abstract Configuration getConfiguration(String configResourcePrefix) throws URISyntaxException, IOException;
 
-    private LoggerContext configure(String configResourcePrefix) throws URISyntaxException, IOException {
+    protected LoggerContext configure(String configResourcePrefix) throws URISyntaxException, IOException {
         Configurator.reconfigure(getConfiguration(configResourcePrefix));
         return (LoggerContext) org.apache.logging.log4j.LogManager.getContext(false);
     }
@@ -507,4 +509,31 @@ public abstract class AbstractLog4j1ConfigurationTest {
             System.clearProperty("test.tmpDir");
         }
     }
+
+    public void testGlobalThreshold() throws Exception {
+        try (final LoggerContext ctx = configure("config-1.2/log4j-global-threshold")) {
+            final Configuration config = ctx.getConfiguration();
+            final Filter filter = config.getFilter();
+            assertTrue(filter instanceof ThresholdFilter);
+            ThresholdFilter thresholdFilter = (ThresholdFilter)filter;
+            assertEquals(Level.INFO, thresholdFilter.getLevel());
+            assertEquals(Filter.Result.NEUTRAL, thresholdFilter.getOnMatch());
+            assertEquals(Filter.Result.DENY, thresholdFilter.getOnMismatch());
+            
+            final Logger logger = LogManager.getLogger(PropertiesConfigurationTest.class);
+            // List appender
+            final Appender appender = config.getAppender("LIST");
+            assertNotNull(appender);
+            final ListAppender legacyAppender = (ListAppender) ((Adapter) appender).getAppender();
+            // Stopped by root logger level
+            logger.trace("TRACE");
+            assertEquals(0, legacyAppender.getEvents().size());
+            // Stopped by global threshold
+            logger.debug("DEBUG");
+            assertEquals(0, legacyAppender.getEvents().size());
+            // Accepted
+            logger.info("INFO");
+            assertEquals(1, legacyAppender.getEvents().size());
+        }
+    }
 }
diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/config/Log4j1ConfigurationFactoryTest.java b/log4j-1.2-api/src/test/java/org/apache/log4j/config/Log4j1ConfigurationFactoryTest.java
index af9ab1d..970905c 100644
--- a/log4j-1.2-api/src/test/java/org/apache/log4j/config/Log4j1ConfigurationFactoryTest.java
+++ b/log4j-1.2-api/src/test/java/org/apache/log4j/config/Log4j1ConfigurationFactoryTest.java
@@ -29,11 +29,14 @@ import java.net.URL;
 import org.apache.log4j.layout.Log4j1XmlLayout;
 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.Layout;
+import org.apache.logging.log4j.core.LoggerContext;
 import org.apache.logging.log4j.core.appender.ConsoleAppender;
 import org.apache.logging.log4j.core.appender.ConsoleAppender.Target;
 import org.apache.logging.log4j.core.config.Configuration;
 import org.apache.logging.log4j.core.config.LoggerConfig;
+import org.apache.logging.log4j.core.filter.ThresholdFilter;
 import org.apache.logging.log4j.core.layout.PatternLayout;
 import org.junit.Test;
 
@@ -172,4 +175,17 @@ public class Log4j1ConfigurationFactoryTest extends AbstractLog4j1ConfigurationT
             fail(e.getMessage());
         }
     }
+
+    @Test
+    public void testGlobalThreshold() throws Exception {
+        try (final LoggerContext ctx = configure("config-1.2/log4j-global-threshold")) {
+            final Configuration config = ctx.getConfiguration();
+            final Filter filter = config.getFilter();
+            assertTrue(filter instanceof ThresholdFilter);
+            ThresholdFilter thresholdFilter = (ThresholdFilter)filter;
+            assertEquals(Level.INFO, thresholdFilter.getLevel());
+            assertEquals(Filter.Result.NEUTRAL, thresholdFilter.getOnMatch());
+            assertEquals(Filter.Result.DENY, thresholdFilter.getOnMismatch());
+        }
+    }
 }
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 64a2d5b..cab8e96 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
@@ -311,4 +311,10 @@ public class PropertiesConfigurationTest extends AbstractLog4j1ConfigurationTest
         }
     }
 
+    @Override
+    @Test
+    public void testGlobalThreshold() throws Exception {
+        super.testGlobalThreshold();
+    }
+
 }
diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/config/XmlConfigurationTest.java b/log4j-1.2-api/src/test/java/org/apache/log4j/config/XmlConfigurationTest.java
index 219880e..e1da067 100644
--- a/log4j-1.2-api/src/test/java/org/apache/log4j/config/XmlConfigurationTest.java
+++ b/log4j-1.2-api/src/test/java/org/apache/log4j/config/XmlConfigurationTest.java
@@ -172,4 +172,10 @@ public class XmlConfigurationTest extends AbstractLog4j1ConfigurationTest {
         super.testMultipleFilters();
     }
 
+    @Override
+    @Test
+    public void testGlobalThreshold() throws Exception {
+        super.testGlobalThreshold();
+    }
+
 }
diff --git a/log4j-1.2-api/src/test/resources/config-1.2/log4j-global-threshold.properties b/log4j-1.2-api/src/test/resources/config-1.2/log4j-global-threshold.properties
new file mode 100644
index 0000000..f76d396
--- /dev/null
+++ b/log4j-1.2-api/src/test/resources/config-1.2/log4j-global-threshold.properties
@@ -0,0 +1,20 @@
+#
+# 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.threshold = info
+log4j.appender.LIST = org.apache.log4j.ListAppender
+log4j.rootLogger = debug, LIST
diff --git a/log4j-1.2-api/src/test/resources/config-1.2/log4j-global-threshold.xml b/log4j-1.2-api/src/test/resources/config-1.2/log4j-global-threshold.xml
new file mode 100644
index 0000000..fb969ca
--- /dev/null
+++ b/log4j-1.2-api/src/test/resources/config-1.2/log4j-global-threshold.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  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.
+-->
+<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
+<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" threshold="info">
+  <appender name="LIST" class="org.apache.log4j.ListAppender" />
+
+  <root>
+    <priority value="debug" />
+    <appender-ref ref="LIST" />
+  </root>
+</log4j:configuration>