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

rgoers pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/master by this push:
     new 7b236ceb16 LOG4J2-2956 - Prevent ContextDataFactory error during 
startup
7b236ceb16 is described below

commit 7b236ceb1628dd5d6758d3f323948365d1c59abb
Author: Ralph Goers <[email protected]>
AuthorDate: Thu May 5 18:26:57 2022 +0200

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

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 3d8c4adf91..17e2f4dd1d 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
@@ -23,6 +23,7 @@ import org.apache.logging.log4j.core.ContextDataInjector;
 import org.apache.logging.log4j.core.Filter;
 import org.apache.logging.log4j.core.LogEvent;
 import org.apache.logging.log4j.core.Logger;
+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;
@@ -34,6 +35,7 @@ import org.apache.logging.log4j.plugins.PluginElement;
 import org.apache.logging.log4j.plugins.PluginFactory;
 import org.apache.logging.log4j.util.PerformanceSensitive;
 import org.apache.logging.log4j.util.ReadOnlyStringMap;
+import org.apache.logging.log4j.util.StringMap;
 
 import java.util.Map;
 import java.util.Objects;
@@ -129,6 +131,11 @@ public final class DynamicThresholdFilter extends 
AbstractFilter {
             final String key, final Map<String, Level> pairs, final Level 
defaultLevel,
             final Result onMatch, final Result onMismatch, final 
ContextDataInjector injector) {
         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 2624df8cd5..4efd92eb98 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
@@ -22,6 +22,7 @@ import org.apache.logging.log4j.core.ContextDataInjector;
 import org.apache.logging.log4j.core.Filter;
 import org.apache.logging.log4j.core.LogEvent;
 import org.apache.logging.log4j.core.Logger;
+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;
@@ -36,6 +37,7 @@ import 
org.apache.logging.log4j.plugins.validation.constraints.Required;
 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;
 
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -62,6 +64,11 @@ public class ThreadContextMapFilter extends MapFilter {
             final Map<String, List<String>> pairs, final boolean oper, final 
Result onMatch,
             final Result onMismatch, final ContextDataInjector injector) {
         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