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 17:13:47 UTC

[1/3] logging-log4j2 git commit: Test for NdcPatternConverter

Repository: logging-log4j2
Updated Branches:
  refs/heads/master c0cb21e8a -> b06d5de75


Test for NdcPatternConverter


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

Branch: refs/heads/master
Commit: f32c0fe1e4828bac120ab2554d761affbef8b4aa
Parents: c0cb21e
Author: Mikael St�ldal <mi...@staldal.nu>
Authored: Sat Aug 13 18:56:59 2016 +0200
Committer: Mikael St�ldal <mi...@staldal.nu>
Committed: Sat Aug 13 18:56:59 2016 +0200

----------------------------------------------------------------------
 .../core/pattern/NdcPatternConverterTest.java   | 77 ++++++++++++++++++++
 1 file changed, 77 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f32c0fe1/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/NdcPatternConverterTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/NdcPatternConverterTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/NdcPatternConverterTest.java
new file mode 100644
index 0000000..85b3e85
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/NdcPatternConverterTest.java
@@ -0,0 +1,77 @@
+/*
+ * 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.logging.log4j.core.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.message.Message;
+import org.apache.logging.log4j.message.SimpleMessage;
+import org.junit.After;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class NdcPatternConverterTest {
+
+    @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 Message msg = new SimpleMessage("Hello");
+        final NdcPatternConverter converter = NdcPatternConverter.newInstance(null);
+        final LogEvent event = Log4jLogEvent.newBuilder() //
+                .setLoggerName("MyLogger") //
+                .setLevel(Level.DEBUG) //
+                .setMessage(msg) //
+                .build();
+        final StringBuilder sb = new StringBuilder();
+        converter.format(event, sb);
+        final String str = sb.toString();
+        assertEquals(expected, str);
+    }
+
+    @After
+    public void tearDown() {
+        ThreadContext.clearStack();
+    }
+}
+


Re: [2/3] logging-log4j2 git commit: Clear ThreadContext after test

Posted by Remko Popma <re...@gmail.com>.
The @After public void tearDown() 
currently clears the *stack* while we put data in the *map*. 

Away from PC, but if I remember correctly there is a method ThreadContext.clear () that clears both. That should be fine. 

Sent from my iPhone

> On 2016/08/14, at 2:13, mikes@apache.org wrote:
> 
> Clear ThreadContext after test
> 
> 
> Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
> Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/a40b28f2
> Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/a40b28f2
> Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/a40b28f2
> 
> Branch: refs/heads/master
> Commit: a40b28f26901c5549810d36fa6763a8697207740
> Parents: f32c0fe
> Author: Mikael Ståldal <mi...@staldal.nu>
> Authored: Sat Aug 13 19:00:47 2016 +0200
> Committer: Mikael Ståldal <mi...@staldal.nu>
> Committed: Sat Aug 13 19:00:47 2016 +0200
> 
> ----------------------------------------------------------------------
> .../core/pattern/MdcPatternConverterTest.java   | 26 +++++++++++---------
> 1 file changed, 14 insertions(+), 12 deletions(-)
> ----------------------------------------------------------------------
> 
> 
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/a40b28f2/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MdcPatternConverterTest.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MdcPatternConverterTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MdcPatternConverterTest.java
> index dcacbcf..abdae00 100644
> --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MdcPatternConverterTest.java
> +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MdcPatternConverterTest.java
> @@ -22,6 +22,8 @@ import org.apache.logging.log4j.core.LogEvent;
> import org.apache.logging.log4j.core.impl.Log4jLogEvent;
> import org.apache.logging.log4j.message.Message;
> import org.apache.logging.log4j.message.SimpleMessage;
> +import org.junit.After;
> +import org.junit.Before;
> import org.junit.Test;
> 
> import static org.junit.Assert.*;
> @@ -31,13 +33,16 @@ import static org.junit.Assert.*;
>  */
> public class MdcPatternConverterTest {
> 
> -    @Test
> -    public void testConverter() {
> -
> -        final Message msg = new SimpleMessage("Hello");
> +    @Before
> +    public void setup() {
>         ThreadContext.put("subject", "I");
>         ThreadContext.put("verb", "love");
>         ThreadContext.put("object", "Log4j");
> +    }
> +
> +    @Test
> +    public void testConverter() {
> +        final Message msg = new SimpleMessage("Hello");
>         final MdcPatternConverter converter = MdcPatternConverter.newInstance(null);
>         final LogEvent event = Log4jLogEvent.newBuilder() //
>                 .setLoggerName("MyLogger") //
> @@ -53,12 +58,8 @@ public class MdcPatternConverterTest {
> 
>     @Test
>     public void testConverterWithKey() {
> -
>         final Message msg = new SimpleMessage("Hello");
>         final String [] options = new String[] {"object"};
> -        ThreadContext.put("subject", "I");
> -        ThreadContext.put("verb", "love");
> -        ThreadContext.put("object", "Log4j");
>         final MdcPatternConverter converter = MdcPatternConverter.newInstance(options);
>         final LogEvent event = Log4jLogEvent.newBuilder() //
>                 .setLoggerName("MyLogger") //
> @@ -74,12 +75,8 @@ public class MdcPatternConverterTest {
> 
>     @Test
>     public void testConverterWithKeys() {
> -
>         final Message msg = new SimpleMessage("Hello");
>         final String [] options = new String[] {"object, subject"};
> -        ThreadContext.put("subject", "I");
> -        ThreadContext.put("verb", "love");
> -        ThreadContext.put("object", "Log4j");
>         final MdcPatternConverter converter = MdcPatternConverter.newInstance(options);
>         final LogEvent event = Log4jLogEvent.newBuilder() //
>                 .setLoggerName("MyLogger") //
> @@ -92,5 +89,10 @@ public class MdcPatternConverterTest {
>         final String expected = "{object=Log4j, subject=I}";
>         assertEquals(expected, str);
>     }
> +
> +    @After
> +    public void tearDown() {
> +        ThreadContext.clearStack();
> +    }
> }
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org


[2/3] logging-log4j2 git commit: Clear ThreadContext after test

Posted by mi...@apache.org.
Clear ThreadContext after test


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

Branch: refs/heads/master
Commit: a40b28f26901c5549810d36fa6763a8697207740
Parents: f32c0fe
Author: Mikael St�ldal <mi...@staldal.nu>
Authored: Sat Aug 13 19:00:47 2016 +0200
Committer: Mikael St�ldal <mi...@staldal.nu>
Committed: Sat Aug 13 19:00:47 2016 +0200

----------------------------------------------------------------------
 .../core/pattern/MdcPatternConverterTest.java   | 26 +++++++++++---------
 1 file changed, 14 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/a40b28f2/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MdcPatternConverterTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MdcPatternConverterTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MdcPatternConverterTest.java
index dcacbcf..abdae00 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MdcPatternConverterTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MdcPatternConverterTest.java
@@ -22,6 +22,8 @@ import org.apache.logging.log4j.core.LogEvent;
 import org.apache.logging.log4j.core.impl.Log4jLogEvent;
 import org.apache.logging.log4j.message.Message;
 import org.apache.logging.log4j.message.SimpleMessage;
+import org.junit.After;
+import org.junit.Before;
 import org.junit.Test;
 
 import static org.junit.Assert.*;
@@ -31,13 +33,16 @@ import static org.junit.Assert.*;
  */
 public class MdcPatternConverterTest {
 
-    @Test
-    public void testConverter() {
-
-        final Message msg = new SimpleMessage("Hello");
+    @Before
+    public void setup() {
         ThreadContext.put("subject", "I");
         ThreadContext.put("verb", "love");
         ThreadContext.put("object", "Log4j");
+    }
+
+    @Test
+    public void testConverter() {
+        final Message msg = new SimpleMessage("Hello");
         final MdcPatternConverter converter = MdcPatternConverter.newInstance(null);
         final LogEvent event = Log4jLogEvent.newBuilder() //
                 .setLoggerName("MyLogger") //
@@ -53,12 +58,8 @@ public class MdcPatternConverterTest {
 
     @Test
     public void testConverterWithKey() {
-
         final Message msg = new SimpleMessage("Hello");
         final String [] options = new String[] {"object"};
-        ThreadContext.put("subject", "I");
-        ThreadContext.put("verb", "love");
-        ThreadContext.put("object", "Log4j");
         final MdcPatternConverter converter = MdcPatternConverter.newInstance(options);
         final LogEvent event = Log4jLogEvent.newBuilder() //
                 .setLoggerName("MyLogger") //
@@ -74,12 +75,8 @@ public class MdcPatternConverterTest {
 
     @Test
     public void testConverterWithKeys() {
-
         final Message msg = new SimpleMessage("Hello");
         final String [] options = new String[] {"object, subject"};
-        ThreadContext.put("subject", "I");
-        ThreadContext.put("verb", "love");
-        ThreadContext.put("object", "Log4j");
         final MdcPatternConverter converter = MdcPatternConverter.newInstance(options);
         final LogEvent event = Log4jLogEvent.newBuilder() //
                 .setLoggerName("MyLogger") //
@@ -92,5 +89,10 @@ public class MdcPatternConverterTest {
         final String expected = "{object=Log4j, subject=I}";
         assertEquals(expected, str);
     }
+
+    @After
+    public void tearDown() {
+        ThreadContext.clearStack();
+    }
 }
 


[3/3] logging-log4j2 git commit: Add TODO

Posted by mi...@apache.org.
Add TODO


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

Branch: refs/heads/master
Commit: b06d5de75d22c3dc23d100f90538ff72bb1ec067
Parents: a40b28f
Author: Mikael St�ldal <mi...@staldal.nu>
Authored: Sat Aug 13 19:13:00 2016 +0200
Committer: Mikael St�ldal <mi...@staldal.nu>
Committed: Sat Aug 13 19:13:00 2016 +0200

----------------------------------------------------------------------
 .../java/org/apache/log4j/config/Log4j1ConfigurationFactory.java | 4 ++++
 1 file changed, 4 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/b06d5de7/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 578b2ab..fd509e0 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
@@ -122,6 +122,10 @@ public class Log4j1ConfigurationFactory extends ConfigurationFactory {
             }
             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"));
                 break;
             }