logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rpo...@apache.org
Subject svn commit: r1555076 - in /logging/log4j/log4j2/trunk: log4j-core/src/main/java/org/apache/logging/log4j/core/async/ log4j-core/src/test/java/org/apache/logging/log4j/core/async/ src/changes/ src/site/xdoc/manual/
Date Fri, 03 Jan 2014 12:48:47 GMT
Author: rpopma
Date: Fri Jan  3 12:48:46 2014
New Revision: 1555076

URL: http://svn.apache.org/r1555076
Log:
LOG4J2-467: Added option to toggle Thread name caching in AsyncLogger

Added:
    logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTestCachedThreadName.java
  (with props)
    logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTestUncachedThreadName.java
  (with props)
    logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerThreadNameStrategyTest.java
  (with props)
Modified:
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLogger.java
    logging/log4j/log4j2/trunk/src/changes/changes.xml
    logging/log4j/log4j2/trunk/src/site/xdoc/manual/async.xml

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLogger.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLogger.java?rev=1555076&r1=1555075&r2=1555076&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLogger.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLogger.java
Fri Jan  3 12:48:46 2014
@@ -80,7 +80,30 @@ public class AsyncLogger extends Logger 
     private static final int RINGBUFFER_MIN_SIZE = 128;
     private static final int RINGBUFFER_DEFAULT_SIZE = 256 * 1024;
     private static final StatusLogger LOGGER = StatusLogger.getLogger();
+    private static final ThreadNameStrategy THREAD_NAME_STRATEGY = ThreadNameStrategy.create();
 
+    static enum ThreadNameStrategy { // LOG4J2-467
+        CACHED {
+            public String getThreadName(Info info) {
+                return info.cachedThreadName;
+            }
+        },
+        UNCACHED {
+            public String getThreadName(Info info) {
+                return Thread.currentThread().getName();
+            }
+        };
+        abstract String getThreadName(Info info);
+
+        static ThreadNameStrategy create() {
+            String name = System.getProperty("AsyncLogger.ThreadNameStrategy", CACHED.name());
+            try {
+                return ThreadNameStrategy.valueOf(name);
+            } catch (Exception ex) {
+                return CACHED;
+            }
+        }
+    }
     private static volatile Disruptor<RingBufferLogEvent> disruptor;
     private static Clock clock = ClockFactory.getClock();
 
@@ -89,6 +112,7 @@ public class AsyncLogger extends Logger 
     private static ThreadLocal<Info> threadlocalInfo = new ThreadLocal<Info>();
 
     static {
+        LOGGER.debug("AsyncLogger.ThreadNameStrategy={}", THREAD_NAME_STRATEGY);
         final int ringBufferSize = calculateRingBufferSize();
 
         final WaitStrategy waitStrategy = createWaitStrategy();
@@ -172,18 +196,20 @@ public class AsyncLogger extends Logger 
     /**
      * Tuple with the event translator and thread name for a thread.
      */
-    private static class Info {
-        private RingBufferLogEventTranslator translator;
-        private String cachedThreadName;
+    static class Info {
+        private final RingBufferLogEventTranslator translator;
+        private final String cachedThreadName;
+        public Info(RingBufferLogEventTranslator translator, String threadName) {
+            this.translator = translator;
+            this.cachedThreadName = threadName;
+        }
     }
 
     @Override
     public void log(final Marker marker, final String fqcn, final Level level, final Message
data, final Throwable t) {
         Info info = threadlocalInfo.get();
         if (info == null) {
-            info = new Info();
-            info.translator = new RingBufferLogEventTranslator();
-            info.cachedThreadName = Thread.currentThread().getName();
+            info = new Info(new RingBufferLogEventTranslator(), Thread.currentThread().getName());
             threadlocalInfo.set(info);
         }
 
@@ -200,7 +226,8 @@ public class AsyncLogger extends Logger 
                 ThreadContext.getImmutableStack(), //
 
                 // Thread.currentThread().getName(), //
-                info.cachedThreadName, //
+                // info.cachedThreadName, //
+                THREAD_NAME_STRATEGY.getThreadName(info), // LOG4J2-467
 
                 // location: very expensive operation. LOG4J2-153:
                 // Only include if "includeLocation=true" is specified,

Added: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTestCachedThreadName.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTestCachedThreadName.java?rev=1555076&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTestCachedThreadName.java
(added)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTestCachedThreadName.java
Fri Jan  3 12:48:46 2014
@@ -0,0 +1,75 @@
+/*
+ * 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 static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+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.core.LifeCycle;
+import org.apache.logging.log4j.core.config.ConfigurationFactory;
+import org.apache.logging.log4j.core.helpers.Constants;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class AsyncLoggerTestCachedThreadName {
+
+    @BeforeClass
+    public static void beforeClass() {
+        System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR,
+                AsyncLoggerContextSelector.class.getName());
+        System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY,
+                "AsyncLoggerTest.xml");
+    }
+
+    @AfterClass
+    public static void afterClass() {
+        System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR, "");
+    }
+
+    @Test
+    public void testAsyncLogUsesCachedThreadName() throws Exception {
+        final File f = new File("target", "AsyncLoggerTest.log");
+        // System.out.println(f.getAbsolutePath());
+        f.delete();
+        final Logger log = LogManager.getLogger("com.foo.Bar");
+        final String msg = "Async logger msg";
+        log.info(msg);
+        Thread.currentThread().setName("MODIFIED-THREADNAME");
+        log.info(msg);
+        ((LifeCycle) LogManager.getContext()).stop(); // stop async thread
+
+        final BufferedReader reader = new BufferedReader(new FileReader(f));
+        final String line1 = reader.readLine();
+        final String line2 = reader.readLine();
+        // System.out.println(line1);
+        // System.out.println(line2);
+        reader.close();
+        f.delete();
+        assertNotNull("line1", line1);
+        assertNotNull("line2", line2);
+        assertTrue("line1", line1.endsWith(" INFO c.f.Bar [main]   Async logger msg "));
+        assertTrue("line2", line2.endsWith(" INFO c.f.Bar [main]   Async logger msg "));
+    }
+
+}

Propchange: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTestCachedThreadName.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTestUncachedThreadName.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTestUncachedThreadName.java?rev=1555076&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTestUncachedThreadName.java
(added)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTestUncachedThreadName.java
Fri Jan  3 12:48:46 2014
@@ -0,0 +1,76 @@
+/*
+ * 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 static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+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.core.LifeCycle;
+import org.apache.logging.log4j.core.config.ConfigurationFactory;
+import org.apache.logging.log4j.core.helpers.Constants;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class AsyncLoggerTestUncachedThreadName {
+
+    @BeforeClass
+    public static void beforeClass() {
+        System.setProperty("AsyncLogger.ThreadNameStrategy", "UNCACHED");
+        System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR,
+                AsyncLoggerContextSelector.class.getName());
+        System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY,
+                "AsyncLoggerTest.xml");
+    }
+
+    @AfterClass
+    public static void afterClass() {
+        System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR, "");
+    }
+
+    @Test
+    public void testAsyncLogUsesCurrentThreadName() throws Exception {
+        final File f = new File("target", "AsyncLoggerTest.log");
+        // System.out.println(f.getAbsolutePath());
+        f.delete();
+        final Logger log = LogManager.getLogger("com.foo.Bar");
+        final String msg = "Async logger msg";
+        log.info(msg);
+        Thread.currentThread().setName("MODIFIED-THREADNAME");
+        log.info(msg);
+        ((LifeCycle) LogManager.getContext()).stop(); // stop async thread
+
+        final BufferedReader reader = new BufferedReader(new FileReader(f));
+        final String line1 = reader.readLine();
+        final String line2 = reader.readLine();
+        // System.out.println(line1);
+        // System.out.println(line2);
+        reader.close();
+        f.delete();
+        assertNotNull("line1", line1);
+        assertNotNull("line2", line2);
+        assertTrue("line1", line1.endsWith(" INFO c.f.Bar [main]   Async logger msg "));
+        assertTrue("line2", line2.endsWith(" INFO c.f.Bar [MODIFIED-THREADNAME]   Async logger
msg "));
+    }
+
+}

Propchange: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTestUncachedThreadName.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerThreadNameStrategyTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerThreadNameStrategyTest.java?rev=1555076&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerThreadNameStrategyTest.java
(added)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerThreadNameStrategyTest.java
Fri Jan  3 12:48:46 2014
@@ -0,0 +1,69 @@
+/*
+ * 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 static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class AsyncLoggerThreadNameStrategyTest {
+
+    @Test
+    public void testDefaultThreadNameIsCached() throws Exception {
+        AsyncLogger.ThreadNameStrategy tns = AsyncLogger.ThreadNameStrategy.create();
+        assertSame(AsyncLogger.ThreadNameStrategy.CACHED, tns);
+    }
+
+    @Test
+    public void testUseCachedThreadNameIfInvalidConfig() throws Exception {
+        System.setProperty("AsyncLogger.ThreadNameStrategy", "\\%%InValid ");
+        AsyncLogger.ThreadNameStrategy tns = AsyncLogger.ThreadNameStrategy.create();
+        assertSame(AsyncLogger.ThreadNameStrategy.CACHED, tns);
+    }
+
+    @Test
+    public void testUseUncachedThreadNameIfConfigured() throws Exception {
+        System.setProperty("AsyncLogger.ThreadNameStrategy", "UNCACHED");
+        AsyncLogger.ThreadNameStrategy tns = AsyncLogger.ThreadNameStrategy.create();
+        assertSame(AsyncLogger.ThreadNameStrategy.UNCACHED, tns);
+    }
+
+    @Test
+    public void testUncachedThreadNameStrategyReturnsCurrentThreadName() throws Exception
{
+        AsyncLogger.Info info = new AsyncLogger.Info(null, "original");
+        final String name1 = "MODIFIED-THREADNAME1";
+        Thread.currentThread().setName(name1);
+        assertEquals(name1, AsyncLogger.ThreadNameStrategy.UNCACHED.getThreadName(info));
+
+        final String name2 = "OTHER-THREADNAME2";
+        Thread.currentThread().setName(name2);
+        assertEquals(name2, AsyncLogger.ThreadNameStrategy.UNCACHED.getThreadName(info));
+    }
+
+    @Test
+    public void testCachedThreadNameStrategyReturnsCachedThreadName() throws Exception {
+        final String original = "Original-ThreadName";
+        Thread.currentThread().setName(original);
+        AsyncLogger.Info info = new AsyncLogger.Info(null, original);
+        assertEquals(original, AsyncLogger.ThreadNameStrategy.CACHED.getThreadName(info));
+
+        final String name2 = "OTHER-THREADNAME2";
+        Thread.currentThread().setName(name2);
+        assertEquals(original, AsyncLogger.ThreadNameStrategy.CACHED.getThreadName(info));
+    }
+
+}

Propchange: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerThreadNameStrategyTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1555076&r1=1555075&r2=1555076&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Fri Jan  3 12:48:46 2014
@@ -21,6 +21,9 @@
   </properties>
   <body>
     <release version="2.0-RC1" date="2013-MM-DD" description="Bug fixes and enhancements">
+      <action issue="LOG4J2-467" dev="rpopma" type="fix" due-to="Anthony Baldocchi">
+        Added option to toggle Thread name caching in AsyncLogger.
+      </action>
       <action issue="LOG4J2-478" dev="ggregory" type="fix" due-to="Michael Friedmann.">
         The message and ndc fields are not JavaScript escaped in JSONLayout.
       </action>

Modified: logging/log4j/log4j2/trunk/src/site/xdoc/manual/async.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/site/xdoc/manual/async.xml?rev=1555076&r1=1555075&r2=1555076&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/site/xdoc/manual/async.xml (original)
+++ logging/log4j/log4j2/trunk/src/site/xdoc/manual/async.xml Fri Jan  3 12:48:46 2014
@@ -274,6 +274,21 @@
 						</td>
 					</tr>
 					<tr>
+						<td>AsyncLogger.ThreadNameStrategy</td>
+						<td>
+							<tt>CACHED</tt>
+						</td>
+						<td>
+							Valid values: CACHED, UNCACHED.
+							<br />
+							By default, AsyncLogger caches the thread name
+							in a threadlocal variable to improve performance.
+							Specify the <tt>UNCACHED</tt> option if your application
+							modifies the thread name at runtime (with <tt>Thread.currentThread().setName()</tt>)
+							and you want to see the new thread name reflected in the log.
+						</td>
+					</tr>
+					<tr>
 						<td>log4j.Clock</td>
 						<td>
 							<tt>SystemClock</tt>



Mime
View raw message