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/13 19:25:37 UTC

logging-log4j2 git commit: TTCCLayout for Log4j 1.x compatibility

Repository: logging-log4j2
Updated Branches:
  refs/heads/master 526946eb5 -> ab0ae6a78


TTCCLayout for Log4j 1.x compatibility


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

Branch: refs/heads/master
Commit: ab0ae6a78224da0012aef53402f57161b7b23a1d
Parents: 526946e
Author: Mikael St�ldal <mi...@staldal.nu>
Authored: Sat Aug 13 21:24:33 2016 +0200
Committer: Mikael St�ldal <mi...@staldal.nu>
Committed: Sat Aug 13 21:24:33 2016 +0200

----------------------------------------------------------------------
 .../config/Log4j1ConfigurationFactory.java      | 14 +--
 .../org/apache/log4j/layout/TTCCLayout.java     | 99 ++++++++++++++++++++
 .../config/Log4j1ConfigurationFactoryTest.java  |  5 +-
 .../org/apache/log4j/layout/TTCCLayoutTest.java | 82 ++++++++++++++++
 4 files changed, 191 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ab0ae6a7/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 fc49c2b..5438d3b 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
@@ -55,7 +55,7 @@ import org.apache.logging.log4j.status.StatusLogger;
  * <li>layout = org.apache.log4j.PatternLayout</li>
  * <li>layout = org.apache.log4j.EnhancedPatternLayout (partial?)</li>
  * <li>layout = org.apache.log4j.SimpleLayout</li>
- * <li>layout = org.apache.log4j.TTCCLayout (partial)</li>
+ * <li>layout = org.apache.log4j.TTCCLayout</li>
  * <li>layout = org.apache.log4j.HTMLLayout (partial)</li>
  * <li>layout = org.apache.log4j.xml.XMLLayout (partial)</li>
  * <li>layout.ConversionPattern</li>
@@ -110,6 +110,11 @@ public class Log4j1ConfigurationFactory extends ConfigurationFactory {
             final String cpValue = getLog4jAppenderValue(properties, name, "layout.ConversionPattern", null);
             switch (layoutValue) {
             case "org.apache.log4j.PatternLayout": {
+                // TODO We do not have a %d for the time since the start of the app?
+
+                // TODO Log4j 2's PatternLayout's %NDC is not compatible with Log4j 1's
+                //      Log4j 1: "foo bar baz"
+                //      Log4j 2: "[foo, bar, baz]"
                 appenderBuilder.add(newPatternLayout(builder, cpValue));
                 break;
             }
@@ -122,12 +127,7 @@ public class Log4j1ConfigurationFactory extends ConfigurationFactory {
                 break;
             }
             case "org.apache.log4j.TTCCLayout": {
-                // TODO We do not have a %d for the time since the start of the app?
-
-                // TODO We miss the NDC here, and the Log4j 2's PatternLayout's %NDC is not compatible with Log4j 1's
-                //      Log4j 1: "foo bar baz"
-                //      Log4j 2: "[foo, bar, baz]"
-                appenderBuilder.add(newPatternLayout(builder, "%relative [%threadName] %level %logger - %m%n"));
+                appenderBuilder.add(builder.newLayout("TTCCLayout"));
                 break;
             }
             case "org.apache.log4j.HTMLLayout": {

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ab0ae6a7/log4j-1.2-api/src/main/java/org/apache/log4j/layout/TTCCLayout.java
----------------------------------------------------------------------
diff --git a/log4j-1.2-api/src/main/java/org/apache/log4j/layout/TTCCLayout.java b/log4j-1.2-api/src/main/java/org/apache/log4j/layout/TTCCLayout.java
new file mode 100644
index 0000000..53bd6db
--- /dev/null
+++ b/log4j-1.2-api/src/main/java/org/apache/log4j/layout/TTCCLayout.java
@@ -0,0 +1,99 @@
+/*
+ * 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.layout;
+
+import org.apache.logging.log4j.core.Layout;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.config.Node;
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.core.config.plugins.PluginFactory;
+import org.apache.logging.log4j.core.layout.AbstractStringLayout;
+import org.apache.logging.log4j.core.layout.ByteBufferDestination;
+import org.apache.logging.log4j.core.util.Constants;
+import org.apache.logging.log4j.message.Message;
+import org.apache.logging.log4j.util.StringBuilderFormattable;
+
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+
+/**
+ * Port of TTCCLayout in Log4j 1.x. Provided for compatibility with existing Log4j 1 configurations.
+ *
+ * Originally developed by Ceki G&uuml;lc&uuml;, Heinz Richter, Christopher Williams, Mathias Bogaert.
+ */
+@Plugin(name = "TTCCLayout", category = Node.CATEGORY, elementType = Layout.ELEMENT_TYPE, printObject = true)
+public final class TTCCLayout extends AbstractStringLayout {
+
+    final long startTime = System.currentTimeMillis();
+
+    @PluginFactory
+    public static TTCCLayout createLayout() {
+        return new TTCCLayout();
+    }
+
+    private TTCCLayout() {
+        super(StandardCharsets.UTF_8);
+    }
+
+    @Override
+    public void encode(final LogEvent event, final ByteBufferDestination destination) {
+        final StringBuilder text = getStringBuilder();
+        formatTo(event, text);
+        getStringBuilderEncoder().encode(text, destination);
+    }
+
+    @Override
+    public String toSerializable(final LogEvent event) {
+        final StringBuilder text = getStringBuilder();
+        formatTo(event, text);
+        return text.toString();
+    }
+
+    private void formatTo(final LogEvent event, final StringBuilder buf) {
+        buf.append(event.getTimeMillis() - startTime);
+        buf.append(' ');
+
+        buf.append('[');
+        buf.append(event.getThreadName());
+        buf.append("] ");
+
+        buf.append(event.getLevel().toString());
+        buf.append(' ');
+
+        buf.append(event.getLoggerName());
+        buf.append(' ');
+
+        List<String> ndc = event.getContextStack().asList();
+        if (!ndc.isEmpty()) {
+            for (String ndcElement : ndc) {
+                buf.append(ndcElement);
+                buf.append(' ');
+            }
+        }
+
+        buf.append("- ");
+        final Message message = event.getMessage();
+        if (message instanceof StringBuilderFormattable) {
+            ((StringBuilderFormattable)message).formatTo(buf);
+        } else {
+            buf.append(message.getFormattedMessage());
+        }
+
+        buf.append(Constants.LINE_SEPARATOR);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ab0ae6a7/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 4d637b2..e4330ca 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
@@ -18,6 +18,7 @@ package org.apache.log4j.config;
 
 import java.net.URL;
 
+import org.apache.log4j.layout.TTCCLayout;
 import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.core.Layout;
 import org.apache.logging.log4j.core.appender.ConsoleAppender;
@@ -76,8 +77,8 @@ public class Log4j1ConfigurationFactoryTest {
 
     @Test
     public void testConsoleTtccLayout() throws Exception {
-        final PatternLayout layout = (PatternLayout) testConsole("config-1.2/log4j-console-TTCCLayout.properties");
-        Assert.assertEquals("%relative [%threadName] %level %logger - %m%n", layout.getConversionPattern());
+        final Layout<?> layout = testConsole("config-1.2/log4j-console-TTCCLayout.properties");
+        Assert.assertTrue(layout instanceof TTCCLayout);
     }
 
     @Test

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ab0ae6a7/log4j-1.2-api/src/test/java/org/apache/log4j/layout/TTCCLayoutTest.java
----------------------------------------------------------------------
diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/layout/TTCCLayoutTest.java b/log4j-1.2-api/src/test/java/org/apache/log4j/layout/TTCCLayoutTest.java
new file mode 100644
index 0000000..1f548a8
--- /dev/null
+++ b/log4j-1.2-api/src/test/java/org/apache/log4j/layout/TTCCLayoutTest.java
@@ -0,0 +1,82 @@
+/*
+ * 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.layout;
+
+import org.apache.logging.log4j.ThreadContext;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.message.SimpleMessage;
+import org.apache.logging.log4j.spi.DefaultThreadContextStack;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class TTCCLayoutTest {
+
+    @Test
+    public void test0() {
+        DefaultThreadContextStack contextStack = new DefaultThreadContextStack(true);
+        contextStack.clear();
+        test(contextStack, "");
+    }
+
+    @Test
+    public void test1() {
+        DefaultThreadContextStack contextStack = new DefaultThreadContextStack(true);
+        contextStack.clear();
+        contextStack.push("foo");
+        test(contextStack, "foo ");
+    }
+
+    @Test
+    public void test2() {
+        DefaultThreadContextStack contextStack = new DefaultThreadContextStack(true);
+        contextStack.clear();
+        contextStack.push("foo");
+        contextStack.push("bar");
+        test(contextStack, "foo bar ");
+    }
+
+    private void test(ThreadContext.ContextStack contextStack, String stackOutput) {
+        TTCCLayout layout = TTCCLayout.createLayout();
+
+        Log4jLogEvent event = Log4jLogEvent.newBuilder()
+                .setLoggerName("a.B")
+                .setLevel(org.apache.logging.log4j.Level.DEBUG)
+                .setMessage(new SimpleMessage("Msg"))
+                .setContextStack(contextStack)
+                .setTimeMillis(System.currentTimeMillis() + 17).build();
+
+        String result = layout.toSerializable(event);
+
+        String expected = String.valueOf(event.getTimeMillis() - layout.startTime) +
+                ' ' +
+                '[' +
+                event.getThreadName() +
+                "] " +
+                event.getLevel().toString() +
+                ' ' +
+                event.getLoggerName() +
+                ' ' +
+                stackOutput +
+                "- " +
+                event.getMessage() +
+                System.getProperty("line.separator");
+
+        assertEquals(expected, result);
+    }
+
+}