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 2019/12/07 07:06:29 UTC

[logging-log4j2] branch master updated: LOG4J2-2635 - Wrong java version check in ThreadNameCachingStrategy

This is an automated email from the ASF dual-hosted git repository.

rgoers pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/master by this push:
     new e5cf573  LOG4J2-2635 - Wrong java version check in ThreadNameCachingStrategy
e5cf573 is described below

commit e5cf573900cdd89b9f6cbcf7bc1e11088a1f9075
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Sat Dec 7 00:06:14 2019 -0700

    LOG4J2-2635 - Wrong java version check in ThreadNameCachingStrategy
---
 .../core/async/ThreadNameCachingStrategy.java      | 37 ++++++++++++++++------
 .../async/AsyncLoggerThreadNameStrategyTest.java   | 12 +++----
 src/changes/changes.xml                            |  3 ++
 3 files changed, 35 insertions(+), 17 deletions(-)

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 def4d8d..32e15e8 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
@@ -17,7 +17,11 @@
 
 package org.apache.logging.log4j.core.async;
 
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
 import org.apache.logging.log4j.status.StatusLogger;
+import org.apache.logging.log4j.util.Constants;
 import org.apache.logging.log4j.util.PropertiesUtil;
 
 /**
@@ -44,24 +48,39 @@ public enum ThreadNameCachingStrategy { // LOG4J2-467
 
     private static final StatusLogger LOGGER = StatusLogger.getLogger();
     private static final ThreadLocal<String> THREADLOCAL_NAME = new ThreadLocal<>();
+    static final ThreadNameCachingStrategy DEFAULT_STRATEGY = isAllocatingThreadGetName() ? CACHED : UNCACHED;
 
     abstract String getThreadName();
 
     public static ThreadNameCachingStrategy create() {
-        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 != null ? name : defaultStrategy);
+            final ThreadNameCachingStrategy result = name != null ? ThreadNameCachingStrategy.valueOf(name) : DEFAULT_STRATEGY;
             LOGGER.debug("AsyncLogger.ThreadNameStrategy={} (user specified {}, default is {})",
-                    result, name, defaultStrategy);
+                         result.name(), name, DEFAULT_STRATEGY.name());
             return result;
         } catch (final Exception ex) {
             LOGGER.debug("Using AsyncLogger.ThreadNameStrategy.{}: '{}' not valid: {}",
-                    defaultStrategy, name, ex.toString());
-            return ThreadNameCachingStrategy.valueOf(defaultStrategy);
+                         DEFAULT_STRATEGY.name(), name, ex.toString());
+            return DEFAULT_STRATEGY;
+        }
+    }
+
+    static boolean isAllocatingThreadGetName() {
+        // LOG4J2-2052, LOG4J2-2635 JDK 8u102 ("1.8.0_102") removed the String allocation in Thread.getName()
+        if (Constants.JAVA_MAJOR_VERSION == 8) {
+            try {
+                Pattern javaVersionPattern = Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)_(\\d+)");
+                Matcher m = javaVersionPattern.matcher(System.getProperty("java.version"));
+                if (m.matches()) {
+                    return Integer.parseInt(m.group(3)) == 0 && Integer.parseInt(m.group(4)) < 102;
+                }
+                return true;
+            } catch (Exception e) {
+                return true;
+            }
+        } else {
+            return Constants.JAVA_MAJOR_VERSION < 8;
         }
     }
-}
+}
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 546cc68..bc5945e 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
@@ -26,10 +26,6 @@ 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");
@@ -41,16 +37,16 @@ public class AsyncLoggerThreadNameStrategyTest {
     }
 
     @Test
-    public void testDefaultThreadNameIsCached() throws Exception {
+    public void testDefaultIfNotConfigured() throws Exception {
         final ThreadNameCachingStrategy tns = ThreadNameCachingStrategy.create();
-        assertSame(ThreadNameCachingStrategy.valueOf(DEFAULT_STRATEGY), tns);
+        assertSame(ThreadNameCachingStrategy.DEFAULT_STRATEGY, tns);
     }
 
     @Test
-    public void testUseCachedThreadNameIfInvalidConfig() throws Exception {
+    public void testDefaultIfInvalidConfig() throws Exception {
         System.setProperty("AsyncLogger.ThreadNameStrategy", "\\%%InValid ");
         final ThreadNameCachingStrategy tns = ThreadNameCachingStrategy.create();
-        assertSame(ThreadNameCachingStrategy.valueOf(DEFAULT_STRATEGY), tns);
+        assertSame(ThreadNameCachingStrategy.DEFAULT_STRATEGY, tns);
     }
 
     @Test
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index c577e4e..581a0b8 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -160,6 +160,9 @@
       </action>
     </release>
     <release version="2.13.0" date="2019-MM-DD" description="GA Release 2.13.0">
+      <action issue="LOG4J2-2635" dev="rgoers" type="fix" due-to="Filipp Gunbin">
+        Wrong java version check in ThreadNameCachingStrategy.
+      </action>
       <action issue="LOG4J2-2674" dev="rgoers" type="fix" due-to="Anton Korenkov">
         Use a less confusing name for the CompositeConfiguration source.
       </action>