You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2012/10/01 20:36:53 UTC

svn commit: r1392505 - in /logging/log4j/log4j2/trunk: api/src/main/java/org/apache/logging/log4j/ThreadContext.java api/src/test/java/org/apache/logging/log4j/NoopThreadContextTest.java core/pom.xml pom.xml src/changes/changes.xml

Author: rgoers
Date: Mon Oct  1 18:36:53 2012
New Revision: 1392505

URL: http://svn.apache.org/viewvc?rev=1392505&view=rev
Log:
Implement change requested in LOG4J2-83 and LOG4J2-84. Uses System Properties to disable puts and pushes to the ThreadContext

Added:
    logging/log4j/log4j2/trunk/api/src/test/java/org/apache/logging/log4j/NoopThreadContextTest.java
Modified:
    logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/ThreadContext.java
    logging/log4j/log4j2/trunk/core/pom.xml
    logging/log4j/log4j2/trunk/pom.xml
    logging/log4j/log4j2/trunk/src/changes/changes.xml

Modified: logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/ThreadContext.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/ThreadContext.java?rev=1392505&r1=1392504&r2=1392505&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/ThreadContext.java (original)
+++ logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/ThreadContext.java Mon Oct  1 18:36:53 2012
@@ -39,6 +39,19 @@ public final class ThreadContext {
      */
     public static final Map<String, String> EMPTY_MAP = new ImmutableMap();
 
+
+    private static final String DISABLE_MAP = "disableThreadContextMap";
+
+    private static final String DISABLE_STACK = "disableThreadContextStack";
+
+    private static final String DISABLE_ALL = "disableThreadContext";
+
+    private static boolean all = Boolean.getBoolean(DISABLE_ALL);
+
+    private static boolean useMap = !(Boolean.getBoolean(DISABLE_MAP) || all);
+
+    private static boolean useStack = !(Boolean.getBoolean(DISABLE_STACK) || all);
+
     /**
      * Empty, immutable ContextStack.
      */
@@ -48,7 +61,7 @@ public final class ThreadContext {
         new InheritableThreadLocal<Map<String, String>>() {
             @Override
             protected Map<String, String> childValue(Map<String, String> parentValue) {
-                return parentValue == null ? null : new HashMap<String, String>(parentValue);
+                return parentValue == null || !useMap ? null : new HashMap<String, String>(parentValue);
             }
         };
 
@@ -69,6 +82,9 @@ public final class ThreadContext {
      * @param value The key value.
      */
     public static void put(String key, String value) {
+        if (!useMap) {
+            return;
+        }
         Map<String, String> map = localMap.get();
         if (map == null) {
             map = new HashMap<String, String>();
@@ -175,7 +191,7 @@ public final class ThreadContext {
      * @param stack The stack to use.
      */
     public static void setStack(Collection<String> stack) {
-        if (stack.size() == 0) {
+        if (stack.size() == 0 || !useStack) {
             return;
         }
         localStack.set(new ThreadContextStack(stack));
@@ -234,6 +250,9 @@ public final class ThreadContext {
      * @param message The new diagnostic context information.
      */
     public static void push(String message) {
+        if (!useStack) {
+            return;
+        }
         ContextStack stack = localStack.get();
         if (stack == null) {
             stack = new ThreadContextStack();

Added: logging/log4j/log4j2/trunk/api/src/test/java/org/apache/logging/log4j/NoopThreadContextTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/api/src/test/java/org/apache/logging/log4j/NoopThreadContextTest.java?rev=1392505&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/api/src/test/java/org/apache/logging/log4j/NoopThreadContextTest.java (added)
+++ logging/log4j/log4j2/trunk/api/src/test/java/org/apache/logging/log4j/NoopThreadContextTest.java Mon Oct  1 18:36:53 2012
@@ -0,0 +1,50 @@
+/*
+ * 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.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import static org.junit.Assert.assertNull;
+
+/**
+ *
+ */
+public class NoopThreadContextTest {
+
+
+
+    @BeforeClass
+    public static void before() {
+        System.setProperty("disableThreadContext", "true");
+    }
+
+    @AfterClass
+    public static void after() {
+        System.clearProperty("disableThreadContext");
+    }
+
+    @Test
+    public void testNoop() {
+        ThreadContext.put("Test", "Test");
+        String value = ThreadContext.get("Test");
+        assertNull("value was saved", value);
+    }
+
+
+}

Modified: logging/log4j/log4j2/trunk/core/pom.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/pom.xml?rev=1392505&r1=1392504&r2=1392505&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/pom.xml (original)
+++ logging/log4j/log4j2/trunk/core/pom.xml Mon Oct  1 18:36:53 2012
@@ -121,13 +121,6 @@
         </executions>
       </plugin>
       <plugin>
-        <groupId>org.apache.maven.plugins</groupId>
-        <artifactId>maven-surefire-plugin</artifactId>
-        <configuration>
-          <forkMode>always</forkMode>
-        </configuration>
-      </plugin>
-      <plugin>
         <groupId>org.codehaus.mojo</groupId>
         <artifactId>exec-maven-plugin</artifactId>
         <version>1.2.1</version>

Modified: logging/log4j/log4j2/trunk/pom.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/pom.xml?rev=1392505&r1=1392504&r2=1392505&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/pom.xml (original)
+++ logging/log4j/log4j2/trunk/pom.xml Mon Oct  1 18:36:53 2012
@@ -272,6 +272,13 @@
       </plugin>
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-surefire-plugin</artifactId>
+        <configuration>
+          <forkMode>always</forkMode>
+        </configuration>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-site-plugin</artifactId>
         <version>3.0</version>
         <dependencies>

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1392505&r1=1392504&r2=1392505&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Mon Oct  1 18:36:53 2012
@@ -23,6 +23,14 @@
 
   <body>
     <release version="2.0-beta2" date="TBD" description="Bug fixes and enhancements">
+      <action issue="LOG4J2-84" dev="rgoers" type="add">
+        If system property "disableThreadContextStack" is set pushes to the ThreadContext will be ignored. If
+        system property "disableThreadContext" is set both puts and pushes will be ignored.
+      </action>
+      <action issue="LOG4J2-83" dev="rgoers" type="add">
+        If system property "disableThreadContextMap" is set puts to the ThreadContext will be ignored. If
+        system property "disableThreadContext" is set both puts and pushes will be ignored.
+      </action>
       <action dev="rgoers" type="add">
         Add support for ANSI colors by adding the highlight and style pattern converters. Fix pattern
         parsing to allow nested patterns.