LOG4J2-1447 inject ContextData from configuration properties before the 
RingBufferLogEvent is passed to the appenders


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/10dad1f0
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/10dad1f0
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/10dad1f0

Branch: 
refs/heads/LOG4J2-1010&LOG4J2-1447-injectable-contextdata&better-datastructure
Commit: 10dad1f00a67bd8f8965c46c6283ae8335201bd7
Parents: 3e95a6e
Author: rpopma <[email protected]>
Authored: Wed Jul 27 01:20:54 2016 +0900
Committer: rpopma <[email protected]>
Committed: Wed Jul 27 01:20:54 2016 +0900

----------------------------------------------------------------------
 .../logging/log4j/core/async/AsyncLogger.java   | 31 +++++++++++++-------
 1 file changed, 20 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/10dad1f0/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLogger.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLogger.java 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLogger.java
index db85f64..0098305 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLogger.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLogger.java
@@ -28,6 +28,7 @@ import org.apache.logging.log4j.core.config.Configuration;
 import org.apache.logging.log4j.core.config.Property;
 import org.apache.logging.log4j.core.config.ReliabilityStrategy;
 import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.core.impl.MutableContextData;
 import org.apache.logging.log4j.core.util.Clock;
 import org.apache.logging.log4j.core.util.ClockFactory;
 import org.apache.logging.log4j.core.util.Constants;
@@ -186,14 +187,9 @@ public class AsyncLogger extends Logger implements 
EventTranslatorVararg<RingBuf
                 // don't construct ThrowableProxy until required
                 thrown,
 
-                // config properties are taken care of in the EventHandler 
thread
-                // in the AsyncLogger#actualAsyncLog method
-
-                // needs shallow copy to be fast (LOG4J2-154)
-                ThreadContext.getImmutableContext(), //
-
                 // needs shallow copy to be fast (LOG4J2-154)
                 ThreadContext.getImmutableStack(), //
+
                 // location (expensive to calculate)
                 calcLocationIfRequested(fqcn), //
                 CLOCK.currentTimeMillis(), //
@@ -268,14 +264,11 @@ public class AsyncLogger extends Logger implements 
EventTranslatorVararg<RingBuf
         final Throwable thrown = (Throwable) args[6];
 
         // needs shallow copy to be fast (LOG4J2-154)
-        final Map<String, String> contextMap = 
ThreadContext.getImmutableContext();
-
-        // needs shallow copy to be fast (LOG4J2-154)
         final ContextStack contextStack = ThreadContext.getImmutableStack();
 
         final Thread currentThread = Thread.currentThread();
         final String threadName = THREAD_NAME_CACHING_STRATEGY.getThreadName();
-        event.setValues(asyncLogger, asyncLogger.getName(), marker, fqcn, 
level, message, thrown, contextMap,
+        event.setValues(asyncLogger, asyncLogger.getName(), marker, fqcn, 
level, message, thrown,
                 contextStack, currentThread.getId(), threadName, 
currentThread.getPriority(), location,
                 CLOCK.currentTimeMillis(), nanoClock.nanoTime());
     }
@@ -299,12 +292,28 @@ public class AsyncLogger extends Logger implements 
EventTranslatorVararg<RingBuf
 
     /**
      * This method is called by the EventHandler that processes the 
RingBufferLogEvent in a separate thread.
+     * Merges the contents of the configuration map into the contextData, 
after replacing any variables in the property
+     * values with the StrSubstitutor-supplied actual values.
      *
      * @param event the event to log
      */
     public void actualAsyncLog(final RingBufferLogEvent event) {
         final Map<Property, Boolean> properties = 
privateConfig.loggerConfig.getProperties();
-        event.mergePropertiesIntoContextMap(properties, 
privateConfig.config.getStrSubstitutor());
+
+        if (properties != null) {
+            MutableContextData contextData = (MutableContextData) 
event.getContextData();
+            for (final Map.Entry<Property, Boolean> entry : 
properties.entrySet()) {
+                final Property prop = entry.getKey();
+                if (contextData.getValue(prop.getName()) != null) {
+                    continue; // contextMap overrides config properties
+                }
+                final String value = entry.getValue() //
+                        ? 
privateConfig.config.getStrSubstitutor().replace(event, prop.getValue()) //
+                        : prop.getValue();
+                contextData.putValue(prop.getName(), prop.getValue());
+            }
+        }
+
         final ReliabilityStrategy strategy = 
privateConfig.loggerConfig.getReliabilityStrategy();
         strategy.log(this, event);
     }

Reply via email to