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 2018/05/30 17:41:03 UTC

[1/2] logging-log4j2 git commit: Fix ConcurrentLoggingWithJsonLayoutTest

Repository: logging-log4j2
Updated Branches:
  refs/heads/master 83d821d42 -> 04c2a9ed1


Fix ConcurrentLoggingWithJsonLayoutTest

The test previously checked the wrong file, then skipped
assertions when the expected file did not exist.


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

Branch: refs/heads/master
Commit: 474916fc42b27ce8c3a31502240cb108d6c677b1
Parents: 83d821d
Author: Carter Kozak <ck...@apache.org>
Authored: Wed May 30 11:41:13 2018 -0400
Committer: Carter Kozak <ck...@apache.org>
Committed: Wed May 30 13:39:26 2018 -0400

----------------------------------------------------------------------
 .../layout/ConcurrentLoggingWithJsonLayoutTest.java  | 15 ++++++---------
 .../src/test/resources/log4j2-json-layout.xml        |  2 +-
 2 files changed, 7 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/474916fc/log4j-layout-jackson-json/src/test/java/org/apache/logging/log4j/jackson/json/layout/ConcurrentLoggingWithJsonLayoutTest.java
----------------------------------------------------------------------
diff --git a/log4j-layout-jackson-json/src/test/java/org/apache/logging/log4j/jackson/json/layout/ConcurrentLoggingWithJsonLayoutTest.java b/log4j-layout-jackson-json/src/test/java/org/apache/logging/log4j/jackson/json/layout/ConcurrentLoggingWithJsonLayoutTest.java
index 5d6f02d..828c80b 100644
--- a/log4j-layout-jackson-json/src/test/java/org/apache/logging/log4j/jackson/json/layout/ConcurrentLoggingWithJsonLayoutTest.java
+++ b/log4j-layout-jackson-json/src/test/java/org/apache/logging/log4j/jackson/json/layout/ConcurrentLoggingWithJsonLayoutTest.java
@@ -17,6 +17,7 @@
 package org.apache.logging.log4j.jackson.json.layout;
 
 import static org.hamcrest.CoreMatchers.endsWith;
+import static org.hamcrest.CoreMatchers.equalTo;
 import static org.hamcrest.CoreMatchers.startsWith;
 import static org.junit.Assert.assertThat;
 
@@ -101,17 +102,13 @@ public class ConcurrentLoggingWithJsonLayoutTest {
         }
 
         // if any error occurred, fail this test
-        if (!thrown.isEmpty()) {
-            throw thrown.get(0);
-        }
+        assertThat(thrown, equalTo(Collections.emptyList()));
 
         // simple test to ensure content is not corrupted
-        if (new File(PATH).exists()) {
-            final List<String> lines = Files.readAllLines(new File(PATH).toPath(), Charset.defaultCharset());
-            for (final String line : lines) {
-                assertThat(line, startsWith("{\"thread\":"));
-                assertThat(line, endsWith("\"threadPriority\":5}"));
-            }
+        final List<String> lines = Files.readAllLines(new File(PATH).toPath(), Charset.defaultCharset());
+        for (final String line : lines) {
+            assertThat(line, startsWith("{\"instant\":{\"epochSecond\":"));
+            assertThat(line, endsWith("\"threadPriority\":5}"));
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/474916fc/log4j-layout-jackson-json/src/test/resources/log4j2-json-layout.xml
----------------------------------------------------------------------
diff --git a/log4j-layout-jackson-json/src/test/resources/log4j2-json-layout.xml b/log4j-layout-jackson-json/src/test/resources/log4j2-json-layout.xml
index c7af4e9..93886b3 100644
--- a/log4j-layout-jackson-json/src/test/resources/log4j2-json-layout.xml
+++ b/log4j-layout-jackson-json/src/test/resources/log4j2-json-layout.xml
@@ -18,7 +18,7 @@
 -->
 <Configuration status="INFO" packages="">
   <Appenders>
-    <File name="stdout" ignoreExceptions="false" fileName="target/test-xml-layout.log" append="false">
+    <File name="stdout" ignoreExceptions="false" fileName="target/test-json-layout.log" append="false">
       <JsonLayout compact="true" eventEol="true" />
     </File>
   </Appenders>


[2/2] logging-log4j2 git commit: [LOG4J2-2345] Test JsonLayout with a parameter with {} in toString

Posted by ck...@apache.org.
[LOG4J2-2345] Test JsonLayout with a parameter with {} in toString


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

Branch: refs/heads/master
Commit: 04c2a9ed1a2da88ac55fb04aafcbbaec60b4091a
Parents: 474916f
Author: Carter Kozak <ck...@apache.org>
Authored: Wed May 30 11:44:14 2018 -0400
Committer: Carter Kozak <ck...@apache.org>
Committed: Wed May 30 13:40:44 2018 -0400

----------------------------------------------------------------------
 .../jackson/json/layout/JsonLayoutTest.java     | 74 ++++++++++++++++++++
 1 file changed, 74 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/04c2a9ed/log4j-layout-jackson-json/src/test/java/org/apache/logging/log4j/jackson/json/layout/JsonLayoutTest.java
----------------------------------------------------------------------
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 f8afb51..6aa5f73 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
@@ -36,12 +36,16 @@ import org.apache.logging.log4j.core.LoggerContext;
 import org.apache.logging.log4j.core.config.Configuration;
 import org.apache.logging.log4j.core.config.ConfigurationFactory;
 import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.core.impl.MutableLogEvent;
 import org.apache.logging.log4j.core.layout.LogEventFixtures;
 import org.apache.logging.log4j.core.lookup.JavaLookup;
 import org.apache.logging.log4j.core.util.KeyValuePair;
 import org.apache.logging.log4j.jackson.AbstractJacksonLayout;
 import org.apache.logging.log4j.jackson.json.Log4jJsonObjectMapper;
+import org.apache.logging.log4j.message.Message;
 import org.apache.logging.log4j.message.ObjectMessage;
+import org.apache.logging.log4j.message.ParameterizedMessage;
+import org.apache.logging.log4j.message.ReusableMessageFactory;
 import org.apache.logging.log4j.message.SimpleMessage;
 import org.apache.logging.log4j.spi.AbstractLogger;
 import org.apache.logging.log4j.test.appender.ListAppender;
@@ -440,6 +444,76 @@ public class JsonLayoutTest {
     }
 
     @Test
+    public void testLayoutMessageWithCurlyBraces() throws Exception {
+        final boolean propertiesAsList = false;
+        final AbstractJacksonLayout layout = JsonLayout.newBuilder()
+                .setLocationInfo(false)
+                .setProperties(false)
+                .setPropertiesAsList(propertiesAsList)
+                .setComplete(false)
+                .setCompact(true)
+                .setEventEol(false)
+                .setCharset(StandardCharsets.UTF_8)
+                .setIncludeStacktrace(true)
+                .build();
+        final Log4jLogEvent expected = Log4jLogEvent.newBuilder()
+                .setLoggerName("a.B")
+                .setLoggerFqcn("f.q.c.n")
+                .setLevel(Level.DEBUG)
+                .setMessage(new ParameterizedMessage("Testing {}", new TestObj()))
+                .setThreadName("threadName")
+                .setTimeMillis(1).build();
+        final String str = layout.toSerializable(expected);
+        final String expectedMessage = "Testing " + TestObj.TO_STRING_VALUE;
+        assertTrue(str, str.contains("\"message\":\"" + expectedMessage + '"'));
+        final Log4jLogEvent actual = new Log4jJsonObjectMapper(propertiesAsList, true, false, false).readValue(str, Log4jLogEvent.class);
+        assertEquals(expectedMessage, actual.getMessage().getFormattedMessage());
+    }
+
+    // Test for LOG4J2-2345
+    @Test
+    public void testReusableLayoutMessageWithCurlyBraces() throws Exception {
+        final boolean propertiesAsList = false;
+        final AbstractJacksonLayout layout = JsonLayout.newBuilder()
+                .setLocationInfo(false)
+                .setProperties(false)
+                .setPropertiesAsList(propertiesAsList)
+                .setComplete(false)
+                .setCompact(true)
+                .setEventEol(false)
+                .setCharset(StandardCharsets.UTF_8)
+                .setIncludeStacktrace(true)
+                .build();
+        Message message = ReusableMessageFactory.INSTANCE.newMessage("Testing {}", new TestObj());
+        try {
+            final Log4jLogEvent expected = Log4jLogEvent.newBuilder()
+                    .setLoggerName("a.B")
+                    .setLoggerFqcn("f.q.c.n")
+                    .setLevel(Level.DEBUG)
+                    .setMessage(message)
+                    .setThreadName("threadName")
+                    .setTimeMillis(1).build();
+            MutableLogEvent mutableLogEvent = new MutableLogEvent();
+            mutableLogEvent.initFrom(expected);
+            final String str = layout.toSerializable(mutableLogEvent);
+            final String expectedMessage = "Testing " + TestObj.TO_STRING_VALUE;
+            assertTrue(str, str.contains("\"message\":\"" + expectedMessage + '"'));
+            final Log4jLogEvent actual = new Log4jJsonObjectMapper(propertiesAsList, true, false, false).readValue(str, Log4jLogEvent.class);
+            assertEquals(expectedMessage, actual.getMessage().getFormattedMessage());
+        } finally {
+            ReusableMessageFactory.release(message);
+        }
+    }
+
+    static class TestObj {
+        static final String TO_STRING_VALUE = "This is my toString {} with curly braces";
+        @Override
+        public String toString() {
+            return TO_STRING_VALUE;
+        }
+    }
+
+    @Test
     public void testLocationOffCompactOffMdcOff() throws Exception {
         this.testAllFeatures(false, false, false, false, false, true);
     }