You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rp...@apache.org on 2016/09/02 14:30:40 UTC

[1/4] logging-log4j2 git commit: LOG4J2-1349 added test

Repository: logging-log4j2
Updated Branches:
  refs/heads/LOG4J2-1349-gcfree-threadcontext 1e4f0ffbe -> 1dbe39e0a


LOG4J2-1349 added 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/c5d8f76b
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/c5d8f76b
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/c5d8f76b

Branch: refs/heads/LOG4J2-1349-gcfree-threadcontext
Commit: c5d8f76b877afe8ba5d5d8ca8deb0a2158a22412
Parents: 1e4f0ff
Author: rpopma <rp...@apache.org>
Authored: Fri Sep 2 23:20:47 2016 +0900
Committer: rpopma <rp...@apache.org>
Committed: Fri Sep 2 23:20:47 2016 +0900

----------------------------------------------------------------------
 ...AsyncLoggerGarbageFreeThreadContextTest.java | 83 ++++++++++++++++++++
 1 file changed, 83 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c5d8f76b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerGarbageFreeThreadContextTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerGarbageFreeThreadContextTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerGarbageFreeThreadContextTest.java
new file mode 100644
index 0000000..da232bd
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerGarbageFreeThreadContextTest.java
@@ -0,0 +1,83 @@
+/*
+ * 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.async;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.ThreadContext;
+import org.apache.logging.log4j.core.CoreLoggerContexts;
+import org.apache.logging.log4j.core.config.ConfigurationFactory;
+import org.apache.logging.log4j.core.util.Constants;
+import org.apache.logging.log4j.util.Unbox;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class AsyncLoggerGarbageFreeThreadContextTest {
+
+    @BeforeClass
+    public static void beforeClass() {
+        System.setProperty("log4j2.garbagefree.threadContextMap", "true");
+        System.setProperty("AsyncLogger.RingBufferSize", "128"); // minimum ringbuffer size
+        System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR,
+                AsyncLoggerContextSelector.class.getName());
+        System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY,
+                "AsyncLoggerThreadContextTest.xml");
+    }
+
+    @AfterClass
+    public static void afterClass() {
+        System.clearProperty(Constants.LOG4J_CONTEXT_SELECTOR);
+    }
+
+    @Test
+    public void testAsyncLogWritesToLog() throws Exception {
+        final File file = new File("target", "AsyncLoggerTest.log");
+        // System.out.println(f.getAbsolutePath());
+        file.delete();
+
+        ThreadContext.push("stackvalue");
+        ThreadContext.put("KEY", "mapvalue");
+
+        final Logger log = LogManager.getLogger("com.foo.Bar");
+        final String msg = "Async logger msg";
+        for (int i = 0; i < 128; i++) {
+            log.info("init message #{}", Unbox.box(i));
+        }
+        log.info(msg, new InternalError("this is not a real error"));
+        CoreLoggerContexts.stopLoggerContext(false, file); // stop async thread
+
+        final BufferedReader reader = new BufferedReader(new FileReader(file));
+        for (int i = 0; i < 128; i++) {
+            reader.readLine();
+        }
+        final String line1 = reader.readLine();
+        reader.close();
+        file.delete();
+        assertNotNull("line1", line1);
+        assertTrue("line1 correct", line1.contains(msg));
+
+        assertTrue("ThreadContext.map", line1.contains("mapvalue"));
+        assertTrue("ThreadContext.stack", line1.contains("stackvalue"));
+    }
+}


[3/4] logging-log4j2 git commit: LOG4J2-1349 bugfix when AsyncLoggerConfig ringbuffer MutableLogEvent initializes from a synchronous MutableLogEvent, it should not keep a reference to the other event's context data but should always copy the contents, si

Posted by rp...@apache.org.
LOG4J2-1349 bugfix when AsyncLoggerConfig ringbuffer MutableLogEvent initializes from a synchronous MutableLogEvent, it should not keep a reference to the other event's context data but should always copy the contents, since the events and their context data object are accessed and modified in different threads


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

Branch: refs/heads/LOG4J2-1349-gcfree-threadcontext
Commit: f2818eb6bf517e6b3e394e37b8a188ab57061981
Parents: 4882847
Author: rpopma <rp...@apache.org>
Authored: Fri Sep 2 23:24:52 2016 +0900
Committer: rpopma <rp...@apache.org>
Committed: Fri Sep 2 23:24:52 2016 +0900

----------------------------------------------------------------------
 .../apache/logging/log4j/core/impl/MutableLogEvent.java  | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f2818eb6/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/MutableLogEvent.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/MutableLogEvent.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/MutableLogEvent.java
index bcdc40d..97cb5d6 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/MutableLogEvent.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/MutableLogEvent.java
@@ -89,11 +89,12 @@ public class MutableLogEvent implements LogEvent, ReusableMessage {
         this.timeMillis = event.getTimeMillis();
         this.thrown = event.getThrown();
         this.thrownProxy = event.getThrownProxy();
-        if (event.getContextData() instanceof MutableContextData) {
-            this.contextData = (MutableContextData) event.getContextData();
-        } else {
-            this.contextData.putAll(event.getContextData());
-        }
+
+        // NOTE: this ringbuffer event SHOULD NOT keep a reference to the specified
+        // thread-local MutableLogEvent's context data, because then two threads would call
+        // ContextData.clear() on the same shared instance, resulting in data corruption.
+        this.contextData.putAll(event.getContextData());
+
         this.contextStack = event.getContextStack();
         this.source = event.isIncludeLocation() ? event.getSource() : null;
         this.threadId = event.getThreadId();


[2/4] logging-log4j2 git commit: LOG4J2-1349 update tests to prove garbage-free context map does not allocate

Posted by rp...@apache.org.
LOG4J2-1349 update tests to prove garbage-free context map does not allocate


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

Branch: refs/heads/LOG4J2-1349-gcfree-threadcontext
Commit: 4882847a464c045d8c54607231c09f99390a4955
Parents: c5d8f76
Author: rpopma <rp...@apache.org>
Authored: Fri Sep 2 23:21:10 2016 +0900
Committer: rpopma <rp...@apache.org>
Committed: Fri Sep 2 23:21:10 2016 +0900

----------------------------------------------------------------------
 log4j-core/src/test/resources/gcFreeLogging.xml               | 2 +-
 log4j-core/src/test/resources/gcFreeMixedSyncAsyncLogging.xml | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4882847a/log4j-core/src/test/resources/gcFreeLogging.xml
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/resources/gcFreeLogging.xml b/log4j-core/src/test/resources/gcFreeLogging.xml
index 81a8055..3a3c07e 100644
--- a/log4j-core/src/test/resources/gcFreeLogging.xml
+++ b/log4j-core/src/test/resources/gcFreeLogging.xml
@@ -47,7 +47,7 @@
     </RandomAccessFile>
   </Appenders>
   <Loggers>
-    <Root level="info" includeLocation="false">
+    <Root level="trace" includeLocation="false">
       <appender-ref ref="Console" level="FATAL" />
       <appender-ref ref="File"/>
       <appender-ref ref="RandomAccessFile"/>

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4882847a/log4j-core/src/test/resources/gcFreeMixedSyncAsyncLogging.xml
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/resources/gcFreeMixedSyncAsyncLogging.xml b/log4j-core/src/test/resources/gcFreeMixedSyncAsyncLogging.xml
index 714ea9f..03d55b5 100644
--- a/log4j-core/src/test/resources/gcFreeMixedSyncAsyncLogging.xml
+++ b/log4j-core/src/test/resources/gcFreeMixedSyncAsyncLogging.xml
@@ -47,7 +47,7 @@
   </Appenders>
   <Loggers>
     <AsyncLogger name="org.apache.logging.log4j.core.GcFreeMixedSyncAyncLoggingTest"
-        level="info" includeLocation="false">
+        level="trace" includeLocation="false">
       <appender-ref ref="Console" level="FATAL" />
       <appender-ref ref="File"/>
       <appender-ref ref="RandomAccessFile"/>
@@ -57,7 +57,7 @@
       <appender-ref ref="MemoryMappedFile"/>
       <appender-ref ref="RandomAccessFileGelf"/>
     </AsyncLogger>
-    <Root level="info" includeLocation="false">
+    <Root level="trace" includeLocation="false">
       <appender-ref ref="Console" level="FATAL" />
       <appender-ref ref="File"/>
       <appender-ref ref="RandomAccessFile"/>


[4/4] logging-log4j2 git commit: LOG4J2-1349 added test

Posted by rp...@apache.org.
LOG4J2-1349 added 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/1dbe39e0
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/1dbe39e0
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/1dbe39e0

Branch: refs/heads/LOG4J2-1349-gcfree-threadcontext
Commit: 1dbe39e0aa3dc0a29b8efc0f21c3cc82f85dd19c
Parents: f2818eb
Author: rpopma <rp...@apache.org>
Authored: Fri Sep 2 23:25:12 2016 +0900
Committer: rpopma <rp...@apache.org>
Committed: Fri Sep 2 23:25:12 2016 +0900

----------------------------------------------------------------------
 .../apache/logging/log4j/core/layout/PatternLayoutTest.java | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/1dbe39e0/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutTest.java
index 185432e..aab5896 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutTest.java
@@ -75,7 +75,7 @@ public class PatternLayoutTest {
     Logger root = ctx.getRootLogger();
 
     @Rule
-    public final ThreadContextRule threadContextRule = new ThreadContextRule(); 
+    public final ThreadContextRule threadContextRule = new ThreadContextRule();
 
     private static class Destination implements ByteBufferDestination {
         ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[2048]);
@@ -203,6 +203,11 @@ public class PatternLayoutTest {
     }
 
     @Test
+    public void testMdcPattern0() throws Exception {
+        testMdcPattern("%m : %X", "Hello : {key1=value1, key2=value2}", true);
+    }
+
+    @Test
     public void testMdcPattern1() throws Exception {
         testMdcPattern("%m : %X", "Hello : {}", false);
     }
@@ -241,7 +246,7 @@ public class PatternLayoutTest {
                 .setIncludeLocation(true)
                 .setMessage(new SimpleMessage("entry")).build();
         final String result1 = new FauxLogger().formatEvent(event1, layout);
-        final String expectSuffix1 = String.format("====== PatternLayoutTest.testPatternSelector:243 entry ======%n");
+        final String expectSuffix1 = String.format("====== PatternLayoutTest.testPatternSelector:248 entry ======%n");
         assertTrue("Unexpected result: " + result1, result1.endsWith(expectSuffix1));
         final LogEvent event2 = Log4jLogEvent.newBuilder() //
                 .setLoggerName(this.getClass().getName()).setLoggerFqcn("org.apache.logging.log4j.core.Logger") //