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/11/03 14:55:00 UTC

[01/10] logging-log4j2 git commit: LOG4J2-1660 Added public method ThreadContext::getThreadContextMap; removed class ThreadContextAccess.

Repository: logging-log4j2
Updated Branches:
  refs/heads/master d906c1a03 -> 6eae62eb2


LOG4J2-1660 Added public method ThreadContext::getThreadContextMap; removed class ThreadContextAccess.


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

Branch: refs/heads/master
Commit: 9b2dad09bad2a2b5fd109237408ac9241a2681f9
Parents: d906c1a
Author: rpopma <rp...@apache.org>
Authored: Thu Nov 3 23:37:24 2016 +0900
Committer: rpopma <rp...@apache.org>
Committed: Thu Nov 3 23:37:24 2016 +0900

----------------------------------------------------------------------
 .../log4j/spi/ReadOnlyThreadContextMap.java     | 80 ++++++++++++++++++++
 .../logging/log4j/ThreadContextAccess.java      | 59 ---------------
 2 files changed, 80 insertions(+), 59 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/9b2dad09/log4j-api/src/main/java/org/apache/logging/log4j/spi/ReadOnlyThreadContextMap.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/spi/ReadOnlyThreadContextMap.java b/log4j-api/src/main/java/org/apache/logging/log4j/spi/ReadOnlyThreadContextMap.java
new file mode 100644
index 0000000..f376ce1
--- /dev/null
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/spi/ReadOnlyThreadContextMap.java
@@ -0,0 +1,80 @@
+package org.apache.logging.log4j.spi;
+
+import java.util.Map;
+
+import org.apache.logging.log4j.ThreadContext;
+import org.apache.logging.log4j.util.StringMap;
+
+/**
+ * Read-only view of the data structure that implements MDC behavior for {@link org.apache.logging.log4j.ThreadContext}.
+ * <p>
+ * {@code ThreadContextMap} implementations that also implement this interface can be accessed
+ * by applications via the {@link ThreadContext#getThreadContextMap()} method.
+ * </p>
+ *
+ * @see ThreadContext#getThreadContextMap()
+ * @since 2.8
+ */
+public interface ReadOnlyThreadContextMap {
+
+    /**
+     * Clears the context.
+     */
+    void clear();
+
+    /**
+     * Determines if the key is in the context.
+     * @param key The key to locate.
+     * @return True if the key is in the context, false otherwise.
+     */
+    boolean containsKey(final String key);
+
+    /**
+     * Gets the context identified by the <code>key</code> parameter.
+     *
+     * <p>This method has no side effects.</p>
+     * @param key The key to locate.
+     * @return The value associated with the key or null.
+     */
+    String get(final String key);
+
+    /**
+     * Gets a non-{@code null} mutable copy of current thread's context Map.
+     * @return a mutable copy of the context.
+     */
+    Map<String, String> getCopy();
+
+    /**
+     * Returns an immutable view on the context Map or {@code null} if the context map is empty.
+     * @return an immutable context Map or {@code null}.
+     */
+    Map<String, String> getImmutableMapOrNull();
+
+    /**
+     * Returns the context data for reading. Note that regardless of whether the returned context data has been
+     * {@linkplain StringMap#freeze() frozen} (made read-only) or not, callers should not attempt to modify
+     * the returned data structure.
+     * <p>
+     * <b>Thread safety note:</b>
+     * </p>
+     * <p>
+     * If this {@code ReadOnlyThreadContextMap} implements {@link CopyOnWrite}, then the returned {@code StringMap} can
+     * safely be passed to another thread: future changes in the underlying context data will not be reflected in the
+     * returned {@code StringMap}.
+     * </p><p>
+     * Otherwise, if this {@code ReadOnlyThreadContextMap} does <em>not</em> implement {@link CopyOnWrite}, then it is
+     * not safe to pass the returned {@code StringMap} to another thread because changes in the underlying context may
+     * be reflected in the returned object. It is the responsibility of the caller to make a copy to pass to another
+     * thread.
+     * </p>
+     *
+     * @return a {@code StringMap} containing context data key-value pairs
+     */
+    StringMap getReadOnlyContextData();
+
+    /**
+     * Returns true if the Map is empty.
+     * @return true if the Map is empty, false otherwise.
+     */
+    boolean isEmpty();
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/9b2dad09/log4j-core/src/main/java/org/apache/logging/log4j/ThreadContextAccess.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/ThreadContextAccess.java b/log4j-core/src/main/java/org/apache/logging/log4j/ThreadContextAccess.java
deleted file mode 100644
index c5c33de..0000000
--- a/log4j-core/src/main/java/org/apache/logging/log4j/ThreadContextAccess.java
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * 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;
-
-import org.apache.logging.log4j.spi.ThreadContextMap;
-import org.apache.logging.log4j.spi.ThreadContextMap2;
-
-/**
- * <em>This class is intended for internal log4j2 usage and should not be used directly by applications.</em>
- * <p>
- * Utility class to access package protected methods in {@code ThreadContext}.
- * </p>
- *
- * @see ThreadContext
- * @since 2.7
- */
-public final class ThreadContextAccess {
-    private ThreadContextAccess() { // this class should not be instantiated
-    }
-
-    /**
-     * Returns the internal data structure used to store thread context key-value pairs.
-     * <p><em>
-     * This method is intended for internal log4j2 usage.
-     * The returned data structure is not intended to be used directly by applications.
-     * </em></p>
-     * @return the internal data structure used to store thread context key-value pairs
-     */
-    public static ThreadContextMap getThreadContextMap() {
-        return ThreadContext.getThreadContextMap();
-    }
-
-    /**
-     * Returns the internal data structure used to store thread context key-value pairs.
-     * <p><em>
-     * This method is intended for internal log4j2 usage.
-     * The returned data structure is not intended to be used directly by applications.
-     * </em></p>
-     * @return the internal data structure used to store thread context key-value pairs
-     */
-    public static ThreadContextMap2 getThreadContextMap2() {
-        return (ThreadContextMap2) ThreadContext.getThreadContextMap();
-    }
-}


[04/10] logging-log4j2 git commit: LOG4J2-1660 javadoc reference to ReadOnlyThreadContextMap

Posted by rp...@apache.org.
LOG4J2-1660 javadoc reference to ReadOnlyThreadContextMap


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

Branch: refs/heads/master
Commit: 86e6565d62b599da6b63291dacb541b22d37c755
Parents: f5c64ea
Author: rpopma <rp...@apache.org>
Authored: Thu Nov 3 23:40:43 2016 +0900
Committer: rpopma <rp...@apache.org>
Committed: Thu Nov 3 23:40:43 2016 +0900

----------------------------------------------------------------------
 .../java/org/apache/logging/log4j/spi/ThreadContextMap.java    | 6 ++++++
 1 file changed, 6 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/86e6565d/log4j-api/src/main/java/org/apache/logging/log4j/spi/ThreadContextMap.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/spi/ThreadContextMap.java b/log4j-api/src/main/java/org/apache/logging/log4j/spi/ThreadContextMap.java
index f0b2df4..00baba7 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/spi/ThreadContextMap.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/spi/ThreadContextMap.java
@@ -18,8 +18,14 @@ package org.apache.logging.log4j.spi;
 
 import java.util.Map;
 
+import org.apache.logging.log4j.ThreadContext;
+
 /**
  * Service provider interface to implement custom MDC behavior for {@link org.apache.logging.log4j.ThreadContext}.
+ * <p>
+ * Since 2.8, {@code ThreadContextMap} implementations that implement the {@link ReadOnlyThreadContextMap} interface
+ * are accessible to applications via the {@link ThreadContext#getThreadContextMap()} method.
+ * </p>
  */
 public interface ThreadContextMap {
 


[08/10] logging-log4j2 git commit: LOG4J2-1660 use public method ThreadContext::getThreadContextMap instead of the (removed) ThreadContextAccess class.

Posted by rp...@apache.org.
LOG4J2-1660 use public method ThreadContext::getThreadContextMap instead of the (removed) ThreadContextAccess class.

Also changes to correctly handle the fact that the default ThreadContextMap does not provide a ReadOnlyThreadContextMap view.


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

Branch: refs/heads/master
Commit: d364463b2914b1795e80028df9ac6961bc5b3e35
Parents: b774f1f
Author: rpopma <rp...@apache.org>
Authored: Thu Nov 3 23:53:09 2016 +0900
Committer: rpopma <rp...@apache.org>
Committed: Thu Nov 3 23:53:09 2016 +0900

----------------------------------------------------------------------
 .../core/async/AbstractAsyncThreadContextTestBase.java    | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/d364463b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AbstractAsyncThreadContextTestBase.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AbstractAsyncThreadContextTestBase.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AbstractAsyncThreadContextTestBase.java
index 560f250..f6aa1d4 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AbstractAsyncThreadContextTestBase.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AbstractAsyncThreadContextTestBase.java
@@ -24,7 +24,6 @@ import java.io.IOException;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.ThreadContext;
-import org.apache.logging.log4j.ThreadContextAccess;
 import org.apache.logging.log4j.ThreadContextTestAccess;
 import org.apache.logging.log4j.core.CoreLoggerContexts;
 import org.apache.logging.log4j.core.config.ConfigurationFactory;
@@ -32,6 +31,7 @@ import org.apache.logging.log4j.core.jmx.RingBufferAdmin;
 import org.apache.logging.log4j.core.util.Constants;
 import org.apache.logging.log4j.spi.DefaultThreadContextMap;
 import org.apache.logging.log4j.spi.LoggerContext;
+import org.apache.logging.log4j.spi.ReadOnlyThreadContextMap;
 import org.apache.logging.log4j.util.Unbox;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
@@ -150,8 +150,7 @@ public abstract class AbstractAsyncThreadContextTestBase {
             } else {
                 ThreadContext.remove("count");
             }
-            final String contextmap = ThreadContextAccess.getThreadContextMap().getClass().getSimpleName();
-            log.info("{} {} {} i={}", contextImpl, contextmap, loggerContextName, Unbox.box(i));
+            log.info("{} {} {} i={}", contextImpl, contextMap(), loggerContextName, Unbox.box(i));
         }
         ThreadContext.pop();
         CoreLoggerContexts.stopLoggerContext(false, files[0]); // stop async thread
@@ -165,6 +164,11 @@ public abstract class AbstractAsyncThreadContextTestBase {
         LogManager.shutdown();
     }
 
+    private static String contextMap() {
+        final ReadOnlyThreadContextMap impl = ThreadContext.getThreadContextMap();
+        return impl == null ? ContextImpl.WEBAPP.implClassSimpleName() : impl.getClass().getSimpleName();
+    }
+
     private void checkResult(final File file, final String loggerContextName) throws IOException {
         final String contextDesc = contextImpl + " " + contextImpl.implClassSimpleName() + " " + loggerContextName;
         try (final BufferedReader reader = new BufferedReader(new FileReader(file))) {


[09/10] logging-log4j2 git commit: LOG4J2-1660 change log

Posted by rp...@apache.org.
LOG4J2-1660 change log


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

Branch: refs/heads/master
Commit: 2aa3e53cada5f10220aa62575a62e03ac9a989cd
Parents: d364463
Author: rpopma <rp...@apache.org>
Authored: Thu Nov 3 23:53:48 2016 +0900
Committer: rpopma <rp...@apache.org>
Committed: Thu Nov 3 23:53:48 2016 +0900

----------------------------------------------------------------------
 src/changes/changes.xml | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/2aa3e53c/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 53c7c36..d017257 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -23,7 +23,10 @@
     <title>Changes</title>
   </properties>
   <body>
-    <release version="2.7.1" date="2016-MM-DD" description="GA Release 2.7.1">
+    <release version="2.8" date="2016-MM-DD" description="GA Release 2.8">
+      <action issue="LOG4J2-1660" dev="rpopma" type="add">
+        Added public method ThreadContext::getThreadContextMap; removed class ThreadContextAccess.
+      </action>
       <action issue="LOG4J2-1658" dev="rpopma" type="fix">
         Prevent NPE in ThreadContextMapFactory::createThreadContextMap when initializing Log4j with Configurator::initialize and the BasicContextSelector is used.
       </action>


[06/10] logging-log4j2 git commit: LOG4J2-1660 use public method ThreadContext::getThreadContextMap instead of the (removed) ThreadContextAccess class.

Posted by rp...@apache.org.
LOG4J2-1660 use public method ThreadContext::getThreadContextMap instead of the (removed) ThreadContextAccess class.

Also rely on the default ThreadContextMap _not_ implementing ReadOnlyThreadContextMap rather than checking whether implementations implement ThreadContextMap2.


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

Branch: refs/heads/master
Commit: c3df21fe5b7b9541920bd7516c70e3d68fa186bc
Parents: 5039278
Author: rpopma <rp...@apache.org>
Authored: Thu Nov 3 23:48:09 2016 +0900
Committer: rpopma <rp...@apache.org>
Committed: Thu Nov 3 23:48:09 2016 +0900

----------------------------------------------------------------------
 .../core/impl/ContextDataInjectorFactory.java   | 22 +++++++++++---------
 1 file changed, 12 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c3df21fe/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ContextDataInjectorFactory.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ContextDataInjectorFactory.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ContextDataInjectorFactory.java
index b0a6023..fb1c330 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ContextDataInjectorFactory.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ContextDataInjectorFactory.java
@@ -16,16 +16,16 @@
  */
 package org.apache.logging.log4j.core.impl;
 
-import org.apache.logging.log4j.ThreadContextAccess;
+import org.apache.logging.log4j.ThreadContext;
 import org.apache.logging.log4j.core.ContextDataInjector;
 import org.apache.logging.log4j.core.LogEvent;
-import org.apache.logging.log4j.util.ReadOnlyStringMap;
 import org.apache.logging.log4j.spi.CopyOnWrite;
-import org.apache.logging.log4j.spi.ThreadContextMap;
-import org.apache.logging.log4j.spi.ThreadContextMap2;
+import org.apache.logging.log4j.spi.DefaultThreadContextMap;
+import org.apache.logging.log4j.spi.ReadOnlyThreadContextMap;
 import org.apache.logging.log4j.status.StatusLogger;
 import org.apache.logging.log4j.util.LoaderUtil;
 import org.apache.logging.log4j.util.PropertiesUtil;
+import org.apache.logging.log4j.util.ReadOnlyStringMap;
 
 /**
  * Factory for ContextDataInjectors. Returns a new {@code ContextDataInjector} instance based on the value of system
@@ -80,13 +80,15 @@ public class ContextDataInjectorFactory {
     }
 
     private static ContextDataInjector createDefaultInjector() {
-        final ThreadContextMap threadContextMap = ThreadContextAccess.getThreadContextMap();
-        if (threadContextMap instanceof CopyOnWrite && threadContextMap instanceof ThreadContextMap2) {
-            return new ThreadContextDataInjector.ForCopyOnWriteThreadContextMap();
+        final ReadOnlyThreadContextMap threadContextMap = ThreadContext.getThreadContextMap();
+
+        // note: map may be null (if legacy custom ThreadContextMap was installed by user)
+        if (threadContextMap instanceof DefaultThreadContextMap || threadContextMap == null) {
+            return new ThreadContextDataInjector.ForDefaultThreadContextMap(); // for non StringMap-based context maps
         }
-        if (threadContextMap instanceof ThreadContextMap2) {
-            return new ThreadContextDataInjector.ForGarbageFreeThreadContextMap();
+        if (threadContextMap instanceof CopyOnWrite) {
+            return new ThreadContextDataInjector.ForCopyOnWriteThreadContextMap();
         }
-        return new ThreadContextDataInjector.ForDefaultThreadContextMap();
+        return new ThreadContextDataInjector.ForGarbageFreeThreadContextMap();
     }
 }


[03/10] logging-log4j2 git commit: LOG4J2-1660 implement the new ReadOnlyThreadContextMap interface

Posted by rp...@apache.org.
LOG4J2-1660 implement the new ReadOnlyThreadContextMap interface


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

Branch: refs/heads/master
Commit: f5c64ea26e5fc7a58a1ed3778607f3f0c8762880
Parents: cdc8bb5
Author: rpopma <rp...@apache.org>
Authored: Thu Nov 3 23:40:02 2016 +0900
Committer: rpopma <rp...@apache.org>
Committed: Thu Nov 3 23:40:02 2016 +0900

----------------------------------------------------------------------
 .../logging/log4j/spi/CopyOnWriteSortedArrayThreadContextMap.java  | 2 +-
 .../logging/log4j/spi/GarbageFreeSortedArrayThreadContextMap.java  | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f5c64ea2/log4j-api/src/main/java/org/apache/logging/log4j/spi/CopyOnWriteSortedArrayThreadContextMap.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/spi/CopyOnWriteSortedArrayThreadContextMap.java b/log4j-api/src/main/java/org/apache/logging/log4j/spi/CopyOnWriteSortedArrayThreadContextMap.java
index 2530dff..d346d0e 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/spi/CopyOnWriteSortedArrayThreadContextMap.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/spi/CopyOnWriteSortedArrayThreadContextMap.java
@@ -34,7 +34,7 @@ import org.apache.logging.log4j.util.PropertiesUtil;
  *
  * @since 2.7
  */
-class CopyOnWriteSortedArrayThreadContextMap implements ThreadContextMap2, CopyOnWrite {
+class CopyOnWriteSortedArrayThreadContextMap implements ReadOnlyThreadContextMap, ThreadContextMap2, CopyOnWrite {
 
     /**
      * Property name ({@value} ) for selecting {@code InheritableThreadLocal} (value "true") or plain

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f5c64ea2/log4j-api/src/main/java/org/apache/logging/log4j/spi/GarbageFreeSortedArrayThreadContextMap.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/spi/GarbageFreeSortedArrayThreadContextMap.java b/log4j-api/src/main/java/org/apache/logging/log4j/spi/GarbageFreeSortedArrayThreadContextMap.java
index afba53c..d0ee369 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/spi/GarbageFreeSortedArrayThreadContextMap.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/spi/GarbageFreeSortedArrayThreadContextMap.java
@@ -34,7 +34,7 @@ import org.apache.logging.log4j.util.SortedArrayStringMap;
  * </p>
  * @since 2.7
  */
-class GarbageFreeSortedArrayThreadContextMap implements ThreadContextMap2  {
+class GarbageFreeSortedArrayThreadContextMap implements ReadOnlyThreadContextMap, ThreadContextMap2  {
 
     /**
      * Property name ({@value} ) for selecting {@code InheritableThreadLocal} (value "true") or plain


[05/10] logging-log4j2 git commit: LOG4J2-1660 make method #getThreadContextMap public, change return type to ReadOnlyThreadContextMap, javadoc

Posted by rp...@apache.org.
LOG4J2-1660 make method #getThreadContextMap public, change return type to ReadOnlyThreadContextMap, javadoc


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

Branch: refs/heads/master
Commit: 50392786b8153782af7b15646d9108e52b86e38c
Parents: 86e6565
Author: rpopma <rp...@apache.org>
Authored: Thu Nov 3 23:43:54 2016 +0900
Committer: rpopma <rp...@apache.org>
Committed: Thu Nov 3 23:43:54 2016 +0900

----------------------------------------------------------------------
 .../org/apache/logging/log4j/ThreadContext.java | 30 ++++++++++++++------
 1 file changed, 22 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/50392786/log4j-api/src/main/java/org/apache/logging/log4j/ThreadContext.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/ThreadContext.java b/log4j-api/src/main/java/org/apache/logging/log4j/ThreadContext.java
index f282397..5242993 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/ThreadContext.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/ThreadContext.java
@@ -30,6 +30,7 @@ import org.apache.logging.log4j.message.ParameterizedMessage;
 import org.apache.logging.log4j.spi.DefaultThreadContextMap;
 import org.apache.logging.log4j.spi.DefaultThreadContextStack;
 import org.apache.logging.log4j.spi.NoOpThreadContextMap;
+import org.apache.logging.log4j.spi.ReadOnlyThreadContextMap;
 import org.apache.logging.log4j.spi.ThreadContextMap;
 import org.apache.logging.log4j.spi.ThreadContextMap2;
 import org.apache.logging.log4j.spi.ThreadContextMapFactory;
@@ -195,6 +196,8 @@ public final class ThreadContext {
     private static boolean useStack;
     private static ThreadContextMap contextMap;
     private static ThreadContextStack contextStack;
+    private static ReadOnlyThreadContextMap readOnlyContextMap;
+
     private static final Logger LOGGER = StatusLogger.getLogger();
 
     static {
@@ -221,6 +224,9 @@ public final class ThreadContext {
         } else {
             contextMap = ThreadContextMapFactory.createThreadContextMap();
         }
+        if (contextMap instanceof ReadOnlyThreadContextMap) {
+            readOnlyContextMap = (ReadOnlyThreadContextMap) contextMap;
+        }
     }
 
     /**
@@ -327,15 +333,23 @@ public final class ThreadContext {
     }
 
     /**
-     * Returns the internal data structure used to store thread context key-value pairs.
-     * <p><em>
-     * This data structure is not intended to be used directly by applications. This method is package protected for
-     * internal log4j2 usage.
-     * </em></p>
-     * @return the internal data structure used to store thread context key-value pairs
+     * Returns a read-only view of the internal data structure used to store thread context key-value pairs,
+     * or {@code null} if the internal data structure does not implement the
+     * {@code ReadOnlyThreadContextMap} interface.
+     * <p>
+     * The {@link DefaultThreadContextMap} implementation does not implement {@code ReadOnlyThreadContextMap}, so by
+     * default this method returns {@code null}.
+     * </p>
+     *
+     * @return the internal data structure used to store thread context key-value pairs or {@code null}
+     * @see ThreadContextMapFactory
+     * @see DefaultThreadContextMap
+     * @see org.apache.logging.log4j.spi.CopyOnWriteSortedArrayThreadContextMap
+     * @see org.apache.logging.log4j.spi.GarbageFreeSortedArrayThreadContextMap
+     * @since 2.8
      */
-    static ThreadContextMap getThreadContextMap() {
-        return contextMap;
+    public static ReadOnlyThreadContextMap getThreadContextMap() {
+        return readOnlyContextMap;
     }
 
     /**


[10/10] logging-log4j2 git commit: LOG4J2-1660 updated site documentation

Posted by rp...@apache.org.
LOG4J2-1660 updated site documentation


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

Branch: refs/heads/master
Commit: 6eae62eb2844eb0b03964122f6c41ad6f02b3ed5
Parents: 2aa3e53
Author: rpopma <rp...@apache.org>
Authored: Thu Nov 3 23:54:48 2016 +0900
Committer: rpopma <rp...@apache.org>
Committed: Thu Nov 3 23:54:48 2016 +0900

----------------------------------------------------------------------
 src/site/xdoc/manual/extending.xml      | 13 +++++++++++++
 src/site/xdoc/manual/thread-context.xml |  6 +++---
 2 files changed, 16 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/6eae62eb/src/site/xdoc/manual/extending.xml
----------------------------------------------------------------------
diff --git a/src/site/xdoc/manual/extending.xml b/src/site/xdoc/manual/extending.xml
index 3b0070c..357bb67 100644
--- a/src/site/xdoc/manual/extending.xml
+++ b/src/site/xdoc/manual/extending.xml
@@ -533,6 +533,19 @@ ListAppender list2 = ListAppender.newBuilder().setName("List1").setEntryPerNewLi
           javadoc for detail.
         </p>
         </subsection>
+        <subsection name="Custom ThreadContextMap implementations">
+          <p>
+            A garbage-free StringMap-based context map can be installed by setting system property log4j2.garbagefree.threadContextMap
+            to true. (Log4j must be <a href="garbagefree.html#Config">enabled</a> to use ThreadLocals.)
+          </p><p>
+            Any custom <tt>ThreadContextMap</tt> implementation can be installed by setting system property
+            log4j2.threadContextMap to the fully qualified class name of the class implementing the
+            ThreadContextMap interface. By also implementing the <tt>ReadOnlyThreadContextMap</tt> interface, your custom
+            ThreadContextMap implementation will be accessible to applications via the
+          <a href="../log4j-api/apidocs/org/apache/logging/log4j/ThreadContext.html#getThreadContextMap()">ThreadContext::getThreadContextMap</a>
+            method.
+          </p>
+        </subsection>
           <subsection name="Custom Plugins">
             <p>See the <a href="plugins.html">Plugins</a> section of the manual.</p>
 <!-- TODO: some documentation here! -->

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/6eae62eb/src/site/xdoc/manual/thread-context.xml
----------------------------------------------------------------------
diff --git a/src/site/xdoc/manual/thread-context.xml b/src/site/xdoc/manual/thread-context.xml
index 5ca874f..dd5b21c 100644
--- a/src/site/xdoc/manual/thread-context.xml
+++ b/src/site/xdoc/manual/thread-context.xml
@@ -167,10 +167,10 @@ try (final CloseableThreadContext.Instance ctc = CloseableThreadContext.put("id"
               Use <code>%x</code> to include the full contents of the <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Stack.html">Stack</a>.
             </li>
           </ul>
-          <h4>Non thread-local context data </h4>
+          <h4>Custom context data injectors for non thread-local context data</h4>
           <p>
-            The ThreadContext allows us to tag logging statements so we can link log entries that were related
-            in some way. The limitation is that this only works for logging done on the same application thread
+            With the ThreadContext logging statements can be tagged so log entries that were related in some way
+            can be linked via these tags. The limitation is that this only works for logging done on the same application thread
             (or child threads when configured).
           </p>
           <p>


[02/10] logging-log4j2 git commit: LOG4J2-1660 javadoc link to an explanation of how the CopyOnWrite interface is used

Posted by rp...@apache.org.
LOG4J2-1660 javadoc link to an explanation of how the CopyOnWrite interface is used


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

Branch: refs/heads/master
Commit: cdc8bb5c97f665437e30a89999519e6b3a31da12
Parents: 9b2dad0
Author: rpopma <rp...@apache.org>
Authored: Thu Nov 3 23:39:24 2016 +0900
Committer: rpopma <rp...@apache.org>
Committed: Thu Nov 3 23:39:24 2016 +0900

----------------------------------------------------------------------
 .../src/main/java/org/apache/logging/log4j/spi/CopyOnWrite.java     | 1 +
 1 file changed, 1 insertion(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/cdc8bb5c/log4j-api/src/main/java/org/apache/logging/log4j/spi/CopyOnWrite.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/spi/CopyOnWrite.java b/log4j-api/src/main/java/org/apache/logging/log4j/spi/CopyOnWrite.java
index 0d9814c..a47eafef 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/spi/CopyOnWrite.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/spi/CopyOnWrite.java
@@ -19,6 +19,7 @@ package org.apache.logging.log4j.spi;
 /**
  * Marker interface indicating that the implementing class is a copy-on-write data structure.
  *
+ * @see ReadOnlyThreadContextMap#getReadOnlyContextData()
  * @since 2.7
  */
 public interface CopyOnWrite {


[07/10] logging-log4j2 git commit: LOG4J2-1660 use public method ThreadContext::getThreadContextMap instead of the (removed) ThreadContextAccess class.

Posted by rp...@apache.org.
LOG4J2-1660 use public method ThreadContext::getThreadContextMap instead of the (removed) ThreadContextAccess class.

Also changes to correctly handle the fact that the default ThreadContextMap does not provide a ReadOnlyThreadContextMap view.


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

Branch: refs/heads/master
Commit: b774f1f264d97daaa8265a55b520a1db729b9660
Parents: c3df21f
Author: rpopma <rp...@apache.org>
Authored: Thu Nov 3 23:52:41 2016 +0900
Committer: rpopma <rp...@apache.org>
Committed: Thu Nov 3 23:52:41 2016 +0900

----------------------------------------------------------------------
 .../log4j/core/impl/ThreadContextDataInjector.java | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/b774f1f2/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThreadContextDataInjector.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThreadContextDataInjector.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThreadContextDataInjector.java
index 192fe4d..25f1d03 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThreadContextDataInjector.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThreadContextDataInjector.java
@@ -21,10 +21,9 @@ import java.util.List;
 import java.util.Map;
 
 import org.apache.logging.log4j.ThreadContext;
-import org.apache.logging.log4j.ThreadContextAccess;
 import org.apache.logging.log4j.core.ContextDataInjector;
 import org.apache.logging.log4j.core.config.Property;
-import org.apache.logging.log4j.spi.ThreadContextMap;
+import org.apache.logging.log4j.spi.ReadOnlyThreadContextMap;
 import org.apache.logging.log4j.util.ReadOnlyStringMap;
 import org.apache.logging.log4j.util.StringMap;
 
@@ -98,11 +97,13 @@ public class ThreadContextDataInjector {
 
         @Override
         public ReadOnlyStringMap rawContextData() {
-            final ThreadContextMap map = ThreadContextAccess.getThreadContextMap();
+            final ReadOnlyThreadContextMap map = ThreadContext.getThreadContextMap();
             if (map instanceof ReadOnlyStringMap) {
                 return (ReadOnlyStringMap) map;
             }
-            return map.isEmpty() ? ContextDataFactory.emptyFrozenContextData() : new JdkMapAdapterStringMap(map.getImmutableMapOrNull());
+            // note: default ThreadContextMap is null
+            final Map<String, String> copy = ThreadContext.getImmutableContext();
+            return copy.isEmpty() ? ContextDataFactory.emptyFrozenContextData() : new JdkMapAdapterStringMap(copy);
         }
     }
 
@@ -128,14 +129,14 @@ public class ThreadContextDataInjector {
             // and such modifications should not be reflected in the log event.
             copyProperties(props, reusable);
 
-            final ReadOnlyStringMap immutableCopy = ThreadContextAccess.getThreadContextMap2().getReadOnlyContextData();
+            final ReadOnlyStringMap immutableCopy = ThreadContext.getThreadContextMap().getReadOnlyContextData();
             reusable.putAll(immutableCopy);
             return reusable;
         }
 
         @Override
         public ReadOnlyStringMap rawContextData() {
-            return ThreadContextAccess.getThreadContextMap2().getReadOnlyContextData();
+            return ThreadContext.getThreadContextMap().getReadOnlyContextData();
         }
     }
 
@@ -161,7 +162,7 @@ public class ThreadContextDataInjector {
         public StringMap injectContextData(final List<Property> props, final StringMap ignore) {
             // If there are no configuration properties we want to just return the ThreadContext's StringMap:
             // it is a copy-on-write data structure so we are sure ThreadContext changes will not affect our copy.
-            final StringMap immutableCopy = ThreadContextAccess.getThreadContextMap2().getReadOnlyContextData();
+            final StringMap immutableCopy = ThreadContext.getThreadContextMap().getReadOnlyContextData();
             if (props == null || props.isEmpty()) {
                 return immutableCopy; // this will replace the LogEvent's context data with the returned instance
             }
@@ -177,7 +178,7 @@ public class ThreadContextDataInjector {
 
         @Override
         public ReadOnlyStringMap rawContextData() {
-            return ThreadContextAccess.getThreadContextMap2().getReadOnlyContextData();
+            return ThreadContext.getThreadContextMap().getReadOnlyContextData();
         }
     }