Repository: logging-log4j2 Updated Branches: refs/heads/master 74384c21c -> 5602fd131
LOG4J2-2052 Disable thread name caching by default when running on Java 8u102 or later. Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/5602fd13 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/5602fd13 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/5602fd13 Branch: refs/heads/master Commit: 5602fd13102d0039ebafe74d03ab3b1235dc4444 Parents: 74384c2 Author: rpopma <[email protected]> Authored: Sun Sep 24 18:50:30 2017 +0900 Committer: rpopma <[email protected]> Committed: Sun Sep 24 18:50:30 2017 +0900 ---------------------------------------------------------------------- .../core/async/ThreadNameCachingStrategy.java | 17 +++++++++----- .../AsyncLoggerThreadNameStrategyTest.java | 24 +++++++++++++++++++- src/changes/changes.xml | 3 +++ 3 files changed, 37 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/5602fd13/log4j-core/src/main/java/org/apache/logging/log4j/core/async/ThreadNameCachingStrategy.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/ThreadNameCachingStrategy.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/ThreadNameCachingStrategy.java index da39978..1741155 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/ThreadNameCachingStrategy.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/ThreadNameCachingStrategy.java @@ -48,15 +48,20 @@ public enum ThreadNameCachingStrategy { // LOG4J2-467 abstract String getThreadName(); public static ThreadNameCachingStrategy create() { - final String name = PropertiesUtil.getProperties().getStringProperty("AsyncLogger.ThreadNameStrategy", - CACHED.name()); + final String defaultStrategy = System.getProperty("java.version").compareTo("1.8.0_102") < 0 + ? "CACHED" // LOG4J2-2052 JDK 8u102 removed the String allocation in Thread.getName() + : "UNCACHED"; + final String name = PropertiesUtil.getProperties().getStringProperty("AsyncLogger.ThreadNameStrategy"); try { - final ThreadNameCachingStrategy result = ThreadNameCachingStrategy.valueOf(name); - LOGGER.debug("AsyncLogger.ThreadNameStrategy={}", result); + final ThreadNameCachingStrategy result = ThreadNameCachingStrategy.valueOf( + name != null ? name : defaultStrategy); + LOGGER.debug("AsyncLogger.ThreadNameStrategy={} (user specified {}, default is {})", + result, name, defaultStrategy); return result; } catch (final Exception ex) { - LOGGER.debug("Using AsyncLogger.ThreadNameStrategy.CACHED: '{}' not valid: {}", name, ex.toString()); - return CACHED; + LOGGER.debug("Using AsyncLogger.ThreadNameStrategy.{}: '{}' not valid: {}", + defaultStrategy, name, ex.toString()); + return ThreadNameCachingStrategy.valueOf(defaultStrategy); } } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/5602fd13/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerThreadNameStrategyTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerThreadNameStrategyTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerThreadNameStrategyTest.java index f6adef4..546cc68 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerThreadNameStrategyTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerThreadNameStrategyTest.java @@ -17,6 +17,8 @@ package org.apache.logging.log4j.core.async; import org.apache.logging.log4j.categories.AsyncLoggers; +import org.junit.After; +import org.junit.Before; import org.junit.Test; import org.junit.experimental.categories.Category; @@ -24,17 +26,37 @@ import static org.junit.Assert.*; @Category(AsyncLoggers.class) public class AsyncLoggerThreadNameStrategyTest { + static final String DEFAULT_STRATEGY = System.getProperty("java.version").compareTo("1.8.0_102") < 0 + ? "CACHED" // LOG4J2-2052 JDK 8u102 removed the String allocation in Thread.getName() + : "UNCACHED"; + + @After + public void after() { + System.clearProperty("AsyncLogger.ThreadNameStrategy"); + } + + @Before + public void before() { + System.clearProperty("AsyncLogger.ThreadNameStrategy"); + } @Test public void testDefaultThreadNameIsCached() throws Exception { final ThreadNameCachingStrategy tns = ThreadNameCachingStrategy.create(); - assertSame(ThreadNameCachingStrategy.CACHED, tns); + assertSame(ThreadNameCachingStrategy.valueOf(DEFAULT_STRATEGY), tns); } @Test public void testUseCachedThreadNameIfInvalidConfig() throws Exception { System.setProperty("AsyncLogger.ThreadNameStrategy", "\\%%InValid "); final ThreadNameCachingStrategy tns = ThreadNameCachingStrategy.create(); + assertSame(ThreadNameCachingStrategy.valueOf(DEFAULT_STRATEGY), tns); + } + + @Test + public void testUseCachedThreadNameIfConfigured() throws Exception { + System.setProperty("AsyncLogger.ThreadNameStrategy", "CACHED"); + final ThreadNameCachingStrategy tns = ThreadNameCachingStrategy.create(); assertSame(ThreadNameCachingStrategy.CACHED, tns); } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/5602fd13/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index e341ebc..4c69603 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -31,6 +31,9 @@ - "remove" - Removed --> <release version="2.9.2" date="2017-XX-XX" description="GA Release 2.9.2"> + <action issue="LOG4J2-2052" dev="rpopma" type="update"> + Disable thread name caching by default when running on Java 8u102 or later. + </action> <action issue="LOG4J2-2055" dev="rgoers" type="fix"> If Log4j is used as the Tomcat logging implementation startup might fail if an application also uses Log4j. </action>
