You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by mi...@apache.org on 2016/08/17 13:22:09 UTC

[4/4] logging-log4j2 git commit: Support %ndc in PatternLayout

Support %ndc in PatternLayout


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

Branch: refs/heads/master
Commit: 211274303961cf0df8ddfd2860f0570af5ed7c6c
Parents: 83c9576
Author: Mikael St�ldal <mi...@staldal.nu>
Authored: Wed Aug 17 15:21:31 2016 +0200
Committer: Mikael St�ldal <mi...@staldal.nu>
Committed: Wed Aug 17 15:21:31 2016 +0200

----------------------------------------------------------------------
 .../config/Log4j1ConfigurationFactory.java      | 34 +++------
 .../pattern/Log4j1NdcPatternConverter.java      | 63 +++++++++++++++++
 .../config/Log4j1ConfigurationFactoryTest.java  |  2 +-
 .../pattern/Log4j1NdcPatternConverterTest.java  | 74 ++++++++++++++++++++
 ...g4j-console-EnhancedPatternLayout.properties |  2 +-
 5 files changed, 150 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/21127430/log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationFactory.java
----------------------------------------------------------------------
diff --git a/log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationFactory.java b/log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationFactory.java
index 19acdaa..427f57f 100644
--- a/log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationFactory.java
+++ b/log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationFactory.java
@@ -51,13 +51,7 @@ import org.apache.logging.log4j.status.StatusLogger;
  * <ul>
  * <li>Follow</li>
  * <li>Target</li>
- * <li>layout = org.apache.log4j.PatternLayout (partial)</li>
- * <li>layout = org.apache.log4j.EnhancedPatternLayout (partial)</li>
- * <li>layout = org.apache.log4j.SimpleLayout</li>
- * <li>layout = org.apache.log4j.TTCCLayout</li>
- * <li>layout = org.apache.log4j.HTMLLayout</li>
- * <li>layout = org.apache.log4j.xml.XMLLayout</li>
- * <li>layout.ConversionPattern</li>
+ * <li>layout</li>
  * </ul>
  * </ul>
  */
@@ -107,26 +101,20 @@ public class Log4j1ConfigurationFactory extends ConfigurationFactory {
         final String layoutValue = getLog4jAppenderValue(properties, name, "layout", null);
         if (layoutValue != null) {
             switch (layoutValue) {
-            case "org.apache.log4j.PatternLayout": {
-                final String pattern = getLog4jAppenderValue(properties, name, "layout.ConversionPattern", null);
-                // TODO Log4j 2's PatternLayout's %x (NDC) is not compatible with Log4j 1's %x
-                //      Log4j 1: "foo bar baz"
-                //      Log4j 2: "[foo, bar, baz]"
-                // TODO Log4j 2's PatternLayout's %X (MDC) is not compatible with Log4j 1's %X
-                //      Log4j 1: "{{foo,bar},{hoo,boo}}"
-                //      Log4j 2: "{foo=bar,hoo=boo}"
-                appenderBuilder.add(newPatternLayout(builder, pattern));
-                break;
-            }
+            case "org.apache.log4j.PatternLayout":
             case "org.apache.log4j.EnhancedPatternLayout": {
                 final String pattern = getLog4jAppenderValue(properties, name, "layout.ConversionPattern", null);
-                // TODO missing %ndc as alias for %NDC
-                // TODO Log4j 2's PatternLayout's %x (NDC) is not compatible with Log4j 1's %x
-                //      Log4j 1: "foo bar baz"
-                //      Log4j 2: "[foo, bar, baz]"
-                // Log4j 2's PatternLayout's %X (MDC) is not compatible with Log4j 1's %X
+
+                // Log4j 2's %x (NDC) is not compatible with Log4j 1's %x
+                //   Log4j 1: "foo bar baz"
+                //   Log4j 2: "[foo, bar, baz]"
+                // Use %ndc to get the Log4j 1 format
+
+                // Log4j 2's %X (MDC) is not compatible with Log4j 1's %X
                 //   Log4j 1: "{{foo,bar}{hoo,boo}}"
                 //   Log4j 2: "{foo=bar,hoo=boo}"
+                // Use %properties to get the Log4j 1 format
+
                 appenderBuilder.add(newPatternLayout(builder, pattern));
                 break;
             }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/21127430/log4j-1.2-api/src/main/java/org/apache/log4j/pattern/Log4j1NdcPatternConverter.java
----------------------------------------------------------------------
diff --git a/log4j-1.2-api/src/main/java/org/apache/log4j/pattern/Log4j1NdcPatternConverter.java b/log4j-1.2-api/src/main/java/org/apache/log4j/pattern/Log4j1NdcPatternConverter.java
new file mode 100644
index 0000000..d3f16f5
--- /dev/null
+++ b/log4j-1.2-api/src/main/java/org/apache/log4j/pattern/Log4j1NdcPatternConverter.java
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.log4j.pattern;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.core.pattern.ConverterKeys;
+import org.apache.logging.log4j.core.pattern.LogEventPatternConverter;
+import org.apache.logging.log4j.core.pattern.PatternConverter;
+
+import java.util.List;
+
+
+/**
+ * Returns the event's NDC in a StringBuilder.
+ */
+@Plugin(name = "Log4j1NdcPatternConverter", category = PatternConverter.CATEGORY)
+@ConverterKeys({ "ndc" })
+public final class Log4j1NdcPatternConverter extends LogEventPatternConverter {
+    /**
+     * Singleton.
+     */
+    private static final Log4j1NdcPatternConverter INSTANCE =
+        new Log4j1NdcPatternConverter();
+
+    /**
+     * Private constructor.
+     */
+    private Log4j1NdcPatternConverter() {
+        super("Log4j1NDC", "ndc");
+    }
+
+    /**
+     * Obtains an instance of NdcPatternConverter.
+     *
+     * @param options options, may be null.
+     * @return instance of NdcPatternConverter.
+     */
+    public static Log4j1NdcPatternConverter newInstance(final String[] options) {
+        return INSTANCE;
+    }
+
+    @Override
+    public void format(final LogEvent event, final StringBuilder toAppendTo) {
+        List<String> ndc = event.getContextStack().asList();
+        toAppendTo.append(StringUtils.join(ndc, ' '));
+    }
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/21127430/log4j-1.2-api/src/test/java/org/apache/log4j/config/Log4j1ConfigurationFactoryTest.java
----------------------------------------------------------------------
diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/config/Log4j1ConfigurationFactoryTest.java b/log4j-1.2-api/src/test/java/org/apache/log4j/config/Log4j1ConfigurationFactoryTest.java
index 51abd59..87fb70b 100644
--- a/log4j-1.2-api/src/test/java/org/apache/log4j/config/Log4j1ConfigurationFactoryTest.java
+++ b/log4j-1.2-api/src/test/java/org/apache/log4j/config/Log4j1ConfigurationFactoryTest.java
@@ -55,7 +55,7 @@ public class Log4j1ConfigurationFactoryTest {
     @Test
     public void testConsoleEnhancedPatternLayout() throws Exception {
         final PatternLayout layout = (PatternLayout) testConsole("config-1.2/log4j-console-EnhancedPatternLayout.properties");
-        assertEquals("%d{ISO8601} [%t][%c] %-5p %properties: %m%n", layout.getConversionPattern());
+        assertEquals("%d{ISO8601} [%t][%c] %-5p %properties %ndc: %m%n", layout.getConversionPattern());
     }
 
     @Test

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/21127430/log4j-1.2-api/src/test/java/org/apache/log4j/pattern/Log4j1NdcPatternConverterTest.java
----------------------------------------------------------------------
diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/pattern/Log4j1NdcPatternConverterTest.java b/log4j-1.2-api/src/test/java/org/apache/log4j/pattern/Log4j1NdcPatternConverterTest.java
new file mode 100644
index 0000000..2f0b80f
--- /dev/null
+++ b/log4j-1.2-api/src/test/java/org/apache/log4j/pattern/Log4j1NdcPatternConverterTest.java
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.log4j.pattern;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.ThreadContext;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.junit.ThreadContextStackRule;
+import org.apache.logging.log4j.message.SimpleMessage;
+import org.junit.Rule;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class Log4j1NdcPatternConverterTest {
+
+    @Rule
+    public final ThreadContextStackRule threadContextRule = new ThreadContextStackRule();
+
+    @Test
+    public void testEmpty() {
+        testConverter("");
+    }
+
+    @Test
+    public void test1() {
+        ThreadContext.push("foo");
+        testConverter("foo");
+    }
+
+    @Test
+    public void test2() {
+        ThreadContext.push("foo");
+        ThreadContext.push("bar");
+        testConverter("foo bar");
+    }
+
+    @Test
+    public void test3() {
+        ThreadContext.push("foo");
+        ThreadContext.push("bar");
+        ThreadContext.push("baz");
+        testConverter("foo bar baz");
+    }
+
+    private void testConverter(final String expected) {
+        final Log4j1NdcPatternConverter converter = Log4j1NdcPatternConverter.newInstance(null);
+        final LogEvent event = Log4jLogEvent.newBuilder()
+                .setLoggerName("MyLogger")
+                .setLevel(Level.DEBUG)
+                .setMessage(new SimpleMessage("Hello"))
+                .build();
+        final StringBuilder sb = new StringBuilder();
+        converter.format(event, sb);
+        assertEquals(expected, sb.toString());
+    }
+
+}
+

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/21127430/log4j-1.2-api/src/test/resources/config-1.2/log4j-console-EnhancedPatternLayout.properties
----------------------------------------------------------------------
diff --git a/log4j-1.2-api/src/test/resources/config-1.2/log4j-console-EnhancedPatternLayout.properties b/log4j-1.2-api/src/test/resources/config-1.2/log4j-console-EnhancedPatternLayout.properties
index 2a233f1..d8099f6 100644
--- a/log4j-1.2-api/src/test/resources/config-1.2/log4j-console-EnhancedPatternLayout.properties
+++ b/log4j-1.2-api/src/test/resources/config-1.2/log4j-console-EnhancedPatternLayout.properties
@@ -14,6 +14,6 @@ log4j.appender.Console=org.apache.log4j.ConsoleAppender
 log4j.appender.Console.ImmediateFlush=false
 log4j.appender.Console.Target=System.err
 log4j.appender.Console.layout=org.apache.log4j.EnhancedPatternLayout
-log4j.appender.Console.layout.ConversionPattern=%d{ISO8601} [%t][%c] %-5p %properties: %m%n
+log4j.appender.Console.layout.ConversionPattern=%d{ISO8601} [%t][%c] %-5p %properties %ndc: %m%n
 
 log4j.logger.com.example.foo = DEBUG