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

rgoers 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 6bc69ab44f LOG4J2-2956 - Prevent ContextDataFactory error during 
startup
6bc69ab44f is described below

commit 6bc69ab44faf962a4df7801b802ad67fca35a635
Author: Ralph Goers <[email protected]>
AuthorDate: Thu May 5 18:27:32 2022 +0200

    LOG4J2-2956 - Prevent ContextDataFactory error during startup
---
 .../apache/logging/log4j/core/filter/DynamicThresholdFilter.java | 7 +++++++
 .../apache/logging/log4j/core/filter/ThreadContextMapFilter.java | 9 ++++++++-
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/DynamicThresholdFilter.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/DynamicThresholdFilter.java
index 5a12aefcd2..31088411ad 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/DynamicThresholdFilter.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/DynamicThresholdFilter.java
@@ -32,11 +32,13 @@ import 
org.apache.logging.log4j.core.config.plugins.PluginAttribute;
 import org.apache.logging.log4j.core.config.plugins.PluginElement;
 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
 import org.apache.logging.log4j.core.ContextDataInjector;
+import org.apache.logging.log4j.core.impl.ContextDataFactory;
 import org.apache.logging.log4j.core.impl.ContextDataInjectorFactory;
 import org.apache.logging.log4j.core.util.KeyValuePair;
 import org.apache.logging.log4j.message.Message;
 import org.apache.logging.log4j.util.PerformanceSensitive;
 import org.apache.logging.log4j.util.ReadOnlyStringMap;
+import org.apache.logging.log4j.util.StringMap;
 
 /**
  * Compares against a log level that is associated with a context value. By 
default the context is the
@@ -80,6 +82,11 @@ public final class DynamicThresholdFilter extends 
AbstractFilter {
     private DynamicThresholdFilter(final String key, final Map<String, Level> 
pairs, final Level defaultLevel,
                                    final Result onMatch, final Result 
onMismatch) {
         super(onMatch, onMismatch);
+        // ContextDataFactory looks up a property. The Spring PropertySource 
may log which will cause recursion.
+        // By initializing the ContextDataFactory here recursion will be 
prevented.
+        StringMap map = ContextDataFactory.createContextData();
+        LOGGER.debug("Successfully initialized ContextDataFactory by 
retrieving the context data with {} entries",
+                map.size());
         Objects.requireNonNull(key, "key cannot be null");
         this.key = key;
         this.levelMap = pairs;
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/ThreadContextMapFilter.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/ThreadContextMapFilter.java
index 7f11e322a7..940709fcd5 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/ThreadContextMapFilter.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/ThreadContextMapFilter.java
@@ -34,12 +34,15 @@ import 
org.apache.logging.log4j.core.config.plugins.PluginAliases;
 import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
 import org.apache.logging.log4j.core.config.plugins.PluginElement;
 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
+import org.apache.logging.log4j.core.impl.ContextDataFactory;
 import org.apache.logging.log4j.core.impl.ContextDataInjectorFactory;
 import org.apache.logging.log4j.core.util.KeyValuePair;
 import org.apache.logging.log4j.message.Message;
+import org.apache.logging.log4j.status.StatusLogger;
 import org.apache.logging.log4j.util.IndexedReadOnlyStringMap;
 import org.apache.logging.log4j.util.PerformanceSensitive;
 import org.apache.logging.log4j.util.ReadOnlyStringMap;
+import org.apache.logging.log4j.util.StringMap;
 
 /**
  * Filter based on a value in the Thread Context Map (MDC).
@@ -48,7 +51,6 @@ import org.apache.logging.log4j.util.ReadOnlyStringMap;
 @PluginAliases("ContextMapFilter")
 @PerformanceSensitive("allocation")
 public class ThreadContextMapFilter extends MapFilter {
-
     private final ContextDataInjector injector = 
ContextDataInjectorFactory.createInjector();
     private final String key;
     private final String value;
@@ -58,6 +60,11 @@ public class ThreadContextMapFilter extends MapFilter {
     public ThreadContextMapFilter(final Map<String, List<String>> pairs, final 
boolean oper, final Result onMatch,
                                   final Result onMismatch) {
         super(pairs, oper, onMatch, onMismatch);
+        // ContextDataFactory looks up a property. The Spring PropertySource 
may log which will cause recursion.
+        // By initializing the ContextDataFactory here recursion will be 
prevented.
+        StringMap map = ContextDataFactory.createContextData();
+        LOGGER.debug("Successfully initialized ContextDataFactory by 
retrieving the context data with {} entries",
+                map.size());
         if (pairs.size() == 1) {
             final Iterator<Map.Entry<String, List<String>>> iter = 
pairs.entrySet().iterator();
             final Map.Entry<String, List<String>> entry = iter.next();

Reply via email to