You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ck...@apache.org on 2022/01/22 01:10:16 UTC

[logging-log4j2] branch master updated: LOG4J2-3358: Fix substitutions when programmatic configuration is used (#726)

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

ckozak 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 c062f55  LOG4J2-3358: Fix substitutions when programmatic configuration is used (#726)
c062f55 is described below

commit c062f55318cac4205c7f7606b8e3091f67999516
Author: Carter Kozak <ck...@apache.org>
AuthorDate: Fri Jan 21 19:55:27 2022 -0500

    LOG4J2-3358: Fix substitutions when programmatic configuration is used (#726)
    
    The problem is that some configuration creation paths fail to
    use the Interpolator constructor which loads all plugins. This
    produces inconsistent behavior based on the configuration construction
    path.
    By merging code-paths, we can provide consistent behavior.
---
 .../logging/log4j/core/lookup/Interpolator.java    | 59 ++--------------------
 .../log4j/jackson/json/layout/JsonLayoutTest.java  | 42 ++++++++++++++-
 src/changes/changes.xml                            |  3 ++
 3 files changed, 48 insertions(+), 56 deletions(-)

diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
index fb705b2..de48103 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
@@ -16,6 +16,7 @@
  */
 package org.apache.logging.log4j.core.lookup;
 
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
@@ -25,7 +26,6 @@ import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.core.LogEvent;
 import org.apache.logging.log4j.core.config.ConfigurationAware;
 import org.apache.logging.log4j.core.util.Constants;
-import org.apache.logging.log4j.core.util.Loader;
 import org.apache.logging.log4j.util.ReflectionUtil;
 import org.apache.logging.log4j.plugins.util.PluginManager;
 import org.apache.logging.log4j.plugins.util.PluginType;
@@ -94,68 +94,17 @@ public class Interpolator extends AbstractConfigurationAwareLookup {
     }
 
     /**
-     * Create the default Interpolator using only Lookups that work without an event.
+     * Create the default Interpolator.
      */
     public Interpolator() {
         this((Map<String, String>) null);
     }
 
     /**
-     * Creates the Interpolator using only Lookups that work without an event and initial properties.
+     * Creates the default Interpolator with the provided properties.
      */
     public Interpolator(final Map<String, String> properties) {
-        this.defaultLookup = new PropertiesLookup(properties);
-        // TODO: this ought to use the PluginManager
-        strLookupMap.put("log4j", new Log4jLookup());
-        strLookupMap.put("sys", new SystemPropertiesLookup());
-        strLookupMap.put("env", new EnvironmentLookup());
-        strLookupMap.put("main", MainMapLookup.MAIN_SINGLETON);
-        strLookupMap.put("map", new MapLookup(properties));
-        strLookupMap.put("marker", new MarkerLookup());
-        strLookupMap.put("java", new JavaLookup());
-        strLookupMap.put("base64", new Base64StrLookup());
-        strLookupMap.put("lower", new LowerLookup());
-        strLookupMap.put("upper", new UpperLookup());
-        // JNDI
-        if (Constants.JNDI_LOOKUP_ENABLED) {
-            try {
-                strLookupMap.put(LOOKUP_KEY_JNDI, Loader.newCheckedInstanceOf(JNDI_LOOKUP, StrLookup.class));
-            } catch (final LinkageError | Exception e) {
-                handleError(LOOKUP_KEY_JNDI, e);
-            }
-        }
-        // JMX input args
-        try {
-            // We might be on Android
-            strLookupMap.put(LOOKUP_KEY_JVMRUNARGS, Loader.newCheckedInstanceOf(JMX_LOOKUP, StrLookup.class));
-        } catch (final LinkageError | Exception e) {
-            handleError(LOOKUP_KEY_JVMRUNARGS, e);
-        }
-        strLookupMap.put("date", new DateLookup());
-        if (Constants.IS_WEB_APP) {
-            try {
-                strLookupMap.put(LOOKUP_KEY_WEB, Loader.newCheckedInstanceOf(WEB_LOOKUP, StrLookup.class));
-            } catch (final Exception ignored) {
-                handleError(LOOKUP_KEY_WEB, ignored);
-            }
-        } else {
-            LOGGER.debug("Not in a ServletContext environment, thus not loading WebLookup plugin.");
-        }
-        try {
-            strLookupMap.put(LOOKUP_KEY_DOCKER, Loader.newCheckedInstanceOf(DOCKER_LOOKUP, StrLookup.class));
-        } catch (final Exception ignored) {
-            handleError(LOOKUP_KEY_DOCKER, ignored);
-        }
-        try {
-            strLookupMap.put(LOOKUP_KEY_SPRING, Loader.newCheckedInstanceOf(SPRING_LOOKUP, StrLookup.class));
-        } catch (final Exception ignored) {
-            handleError(LOOKUP_KEY_SPRING, ignored);
-        }
-        try {
-            strLookupMap.put(LOOKUP_KEY_KUBERNETES, Loader.newCheckedInstanceOf(KUBERNETES_LOOKUP, StrLookup.class));
-        } catch (final Exception | NoClassDefFoundError error) {
-            handleError(LOOKUP_KEY_KUBERNETES, error);
-        }
+        this(new PropertiesLookup(properties), Collections.emptyList());
     }
 
     public StrLookup getDefaultLookup() {
diff --git a/log4j-layout-jackson-json/src/test/java/org/apache/logging/log4j/jackson/json/layout/JsonLayoutTest.java b/log4j-layout-jackson-json/src/test/java/org/apache/logging/log4j/jackson/json/layout/JsonLayoutTest.java
index 20c5514..4be2b5d 100644
--- a/log4j-layout-jackson-json/src/test/java/org/apache/logging/log4j/jackson/json/layout/JsonLayoutTest.java
+++ b/log4j-layout-jackson-json/src/test/java/org/apache/logging/log4j/jackson/json/layout/JsonLayoutTest.java
@@ -32,11 +32,14 @@ import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.ThreadContext;
 import org.apache.logging.log4j.core.test.categories.Layouts;
 import org.apache.logging.log4j.core.Appender;
+import org.apache.logging.log4j.core.LogEvent;
 import org.apache.logging.log4j.core.Logger;
 import org.apache.logging.log4j.core.LoggerContext;
 import org.apache.logging.log4j.core.async.RingBufferLogEvent;
 import org.apache.logging.log4j.core.config.Configuration;
 import org.apache.logging.log4j.core.config.ConfigurationFactory;
+import org.apache.logging.log4j.core.config.DefaultConfiguration;
+import org.apache.logging.log4j.core.impl.ContextDataFactory;
 import org.apache.logging.log4j.core.impl.Log4jLogEvent;
 import org.apache.logging.log4j.core.impl.MutableLogEvent;
 import org.apache.logging.log4j.core.lookup.JavaLookup;
@@ -55,6 +58,7 @@ import org.apache.logging.log4j.message.SimpleMessage;
 import org.apache.logging.log4j.spi.AbstractLogger;
 import org.apache.logging.log4j.core.test.appender.ListAppender;
 import org.apache.logging.log4j.util.SortedArrayStringMap;
+import org.apache.logging.log4j.util.StringMap;
 import org.apache.logging.log4j.util.Strings;
 import org.junit.AfterClass;
 import org.junit.Assert;
@@ -633,7 +637,10 @@ public class JsonLayoutTest {
         return compact ? ":" : " : ";
     }
 
-    @Test   // LOG4J2-2749 (#362)
+    /**
+     * @see <a href="https://issues.apache.org/jira/browse/LOG4J2-2749">LOG4J2-2749</a>
+     */
+    @Test
     public void testEmptyValuesAreIgnored() {
         final AbstractJacksonLayout layout = JsonLayout
                 .newBuilder()
@@ -646,4 +653,37 @@ public class JsonLayoutTest {
         assertFalse(str, str.contains("\"empty\""));
     }
 
+    /**
+     * @see <a href="https://issues.apache.org/jira/browse/LOG4J2-3358">LOG4J2-3358</a>
+     */
+    @Test
+    public void jsonLayout_should_substitute_lookups() {
+
+        // Create the layout.
+        KeyValuePair[] additionalFields = {
+                KeyValuePair
+                        .newBuilder()
+                        .setKey("who")
+                        .setValue("${ctx:WHO}")
+                        .build()
+        };
+        JsonLayout layout = JsonLayout
+                .newBuilder()
+                .setConfiguration(new DefaultConfiguration())
+                .setAdditionalFields(additionalFields)
+                .build();
+
+        // Create a log event containing `WHO` key in MDC.
+        StringMap contextData = ContextDataFactory.createContextData();
+        contextData.putValue("WHO", "mduft");
+        LogEvent logEvent = Log4jLogEvent
+                .newBuilder()
+                .setContextData(contextData)
+                .build();
+
+        // Verify the `WHO` key.
+        String serializedLogEvent = layout.toSerializable(logEvent);
+        assertThat(serializedLogEvent, containsString("\"who\" : \"mduft\""));
+
+    }
 }
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 1513f2d..40a09f5 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -176,6 +176,9 @@
     </release>
     <release version="2.17.2" date="20YY-MM-DD" description="GA Release 2.17.2">
       <!-- FIXES -->
+      <action issue="LOG4J2-3358" dev="ckozak" type="fix">
+        log4j-jpl formats message parameters correctly using MessageFormat.
+      </action>
       <action issue="LOG4J2-3345" dev="ckozak">
         log4j-jpl formats message parameters correctly using MessageFormat.
       </action>