View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  
18  package org.apache.logging.log4j.core.async;
19  
20  import java.util.regex.Matcher;
21  import java.util.regex.Pattern;
22  
23  import org.apache.logging.log4j.status.StatusLogger;
24  import org.apache.logging.log4j.util.Constants;
25  import org.apache.logging.log4j.util.PropertiesUtil;
26  
27  /**
28   * Strategy for deciding whether thread name should be cached or not.
29   */
30  public enum ThreadNameCachingStrategy { // LOG4J2-467
31      CACHED {
32          @Override
33          public String getThreadName() {
34              String result = THREADLOCAL_NAME.get();
35              if (result == null) {
36                  result = Thread.currentThread().getName();
37                  THREADLOCAL_NAME.set(result);
38              }
39              return result;
40          }
41      },
42      UNCACHED {
43          @Override
44          public String getThreadName() {
45              return Thread.currentThread().getName();
46          }
47      };
48  
49      private static final StatusLogger LOGGER = StatusLogger.getLogger();
50      private static final ThreadLocal<String> THREADLOCAL_NAME = new ThreadLocal<>();
51      static final ThreadNameCachingStrategy DEFAULT_STRATEGY = isAllocatingThreadGetName() ? CACHED : UNCACHED;
52  
53      abstract String getThreadName();
54  
55      public static ThreadNameCachingStrategy create() {
56          final String name = PropertiesUtil.getProperties().getStringProperty("AsyncLogger.ThreadNameStrategy");
57          try {
58              final ThreadNameCachingStrategy result = name != null ? ThreadNameCachingStrategy.valueOf(name) : DEFAULT_STRATEGY;
59              LOGGER.debug("AsyncLogger.ThreadNameStrategy={} (user specified {}, default is {})",
60                           result.name(), name, DEFAULT_STRATEGY.name());
61              return result;
62          } catch (final Exception ex) {
63              LOGGER.debug("Using AsyncLogger.ThreadNameStrategy.{}: '{}' not valid: {}",
64                           DEFAULT_STRATEGY.name(), name, ex.toString());
65              return DEFAULT_STRATEGY;
66          }
67      }
68  
69      static boolean isAllocatingThreadGetName() {
70          // LOG4J2-2052, LOG4J2-2635 JDK 8u102 ("1.8.0_102") removed the String allocation in Thread.getName()
71          if (Constants.JAVA_MAJOR_VERSION == 8) {
72              try {
73                  Pattern javaVersionPattern = Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)_(\\d+)");
74                  Matcher m = javaVersionPattern.matcher(System.getProperty("java.version"));
75                  if (m.matches()) {
76                      return Integer.parseInt(m.group(3)) == 0 && Integer.parseInt(m.group(4)) < 102;
77                  }
78                  return true;
79              } catch (Exception e) {
80                  return true;
81              }
82          } else {
83              return Constants.JAVA_MAJOR_VERSION < 8;
84          }
85      }
86  }