You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by vy...@apache.org on 2023/11/01 09:15:18 UTC

(logging-log4j2) 01/02: Fix MDC pattern converter causing issues for `%notEmpty` (#1922)

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

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

commit dd4a88ac2e744440565e5903a9a397f5f82cacfe
Author: Volkan Yazıcı <vo...@yazi.ci>
AuthorDate: Wed Nov 1 10:13:50 2023 +0100

    Fix MDC pattern converter causing issues for `%notEmpty` (#1922)
---
 .../core/pattern/VariablesNotEmptyReplacementConverterTest.java   | 7 ++++++-
 .../apache/logging/log4j/core/pattern/MdcPatternConverter.java    | 2 +-
 src/changelog/.2.x.x/fix_MDC_pattern_converter_for_notEmpty.xml   | 8 ++++++++
 src/site/_release-notes/_2.x.x.adoc                               | 1 +
 4 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/pattern/VariablesNotEmptyReplacementConverterTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/pattern/VariablesNotEmptyReplacementConverterTest.java
index df4b9c2f81..48a2c6c13b 100644
--- a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/pattern/VariablesNotEmptyReplacementConverterTest.java
+++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/pattern/VariablesNotEmptyReplacementConverterTest.java
@@ -68,9 +68,14 @@ public class VariablesNotEmptyReplacementConverterTest {
         testReplacement("%mdc", "");
     }
 
+    @Test
+    void MDC_with_non_existent_key_should_be_replaced() {
+        testReplacement("foo=%mdc{noSuchKey1}", "");
+    }
+
     @Test
     void MDC_with_non_existent_keys_should_be_replaced() {
-        testReplacement("%mdc{noSuchKey1,noSuchKey2}", "");
+        testReplacement("foo=%mdc{noSuchKey1,noSuchKey2}", "");
     }
 
     private void testReplacement(final String tag, final String expectedValue) {
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/MdcPatternConverter.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/MdcPatternConverter.java
index 5fc711e63e..fdc15cbae2 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/MdcPatternConverter.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/MdcPatternConverter.java
@@ -146,7 +146,7 @@ public final class MdcPatternConverter extends LogEventPatternConverter {
 
     @Override
     public String emptyVariableOutput() {
-        return "{}";
+        return key == null ? "{}" : "";
     }
 
 }
diff --git a/src/changelog/.2.x.x/fix_MDC_pattern_converter_for_notEmpty.xml b/src/changelog/.2.x.x/fix_MDC_pattern_converter_for_notEmpty.xml
new file mode 100644
index 0000000000..83250de748
--- /dev/null
+++ b/src/changelog/.2.x.x/fix_MDC_pattern_converter_for_notEmpty.xml
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xmlns="http://logging.apache.org/log4j/changelog"
+       xsi:schemaLocation="http://logging.apache.org/log4j/changelog https://logging.apache.org/log4j/changelog-0.1.2.xsd"
+       type="changed">
+  <issue id="1922" link="https://github.com/apache/logging-log4j2/issues/1922"/>
+  <description format="asciidoc">Fix MDC pattern converter causing issues for `%notEmpty`</description>
+</entry>
diff --git a/src/site/_release-notes/_2.x.x.adoc b/src/site/_release-notes/_2.x.x.adoc
index 8e3c031b30..8610d9dcda 100644
--- a/src/site/_release-notes/_2.x.x.adoc
+++ b/src/site/_release-notes/_2.x.x.adoc
@@ -47,6 +47,7 @@ The module name of four bridges (`log4j-slf4j-impl`, `log4j-slf4j2-impl`, `log4j
 === Changed
 
 * Change the order of evaluation of `FormattedMessage` formatters. Messages are evaluated using `java.util.Format` only if they don't comply to the `java.text.MessageFormat` or `ParameterizedMessage` format. (https://github.com/apache/logging-log4j2/issues/1223[1223])
+* Fix MDC pattern converter causing issues for `%notEmpty` (https://github.com/apache/logging-log4j2/issues/1922[1922])
 * Fix `NotSerializableException` when `Logger` is serialized with a `ReusableMessageFactory`. (https://github.com/apache/logging-log4j2/issues/1884[1884])
 * Update `com.fasterxml.jackson:jackson-bom` to version `2.15.3` (https://github.com/apache/logging-log4j2/pull/1894[1894])
 * Update `com.github.luben:zstd-jni` to version `1.5.5-8` (https://github.com/apache/logging-log4j2/pull/1935[1935])