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 package org.apache.logging.log4j.util; 18 19 /** 20 * Log4j API Constants. 21 * 22 * @since 2.6.2 23 */ 24 public final class Constants { 25 /** 26 * {@code true} if we think we are running in a web container, based on the boolean value of system property 27 * "log4j2.is.webapp", or (if this system property is not set) whether the {@code javax.servlet.Servlet} class 28 * is present in the classpath. 29 */ 30 public static final boolean IS_WEB_APP = PropertiesUtil.getProperties().getBooleanProperty( 31 "log4j2.is.webapp", isClassAvailable("javax.servlet.Servlet")); 32 33 /** 34 * Kill switch for object pooling in ThreadLocals that enables much of the LOG4J2-1270 no-GC behaviour. 35 * <p> 36 * {@code True} for non-{@link #IS_WEB_APP web apps}, disable by setting system property 37 * "log4j2.enable.threadlocals" to "false". 38 * </p> 39 */ 40 public static final boolean ENABLE_THREADLOCALS = !IS_WEB_APP && PropertiesUtil.getProperties().getBooleanProperty( 41 "log4j2.enable.threadlocals", true); 42 43 public static final int JAVA_MAJOR_VERSION = getMajorVersion(); 44 45 /** 46 * Maximum size of the StringBuilders used in RingBuffer LogEvents to store the contents of reusable Messages. 47 * After a large message has been delivered to the appenders, the StringBuilder is trimmed to this size. 48 * <p> 49 * The default value is {@value}, which allows the StringBuilder to resize three times from its initial size. 50 * Users can override with system property "log4j.maxReusableMsgSize". 51 * </p> 52 * @since 2.9 53 */ 54 public static final int MAX_REUSABLE_MESSAGE_SIZE = size("log4j.maxReusableMsgSize", (128 * 2 + 2) * 2 + 2); 55 56 /** 57 * Name of the system property that will turn on TRACE level internal log4j2 status logging. 58 * <p> 59 * If system property {@value} is defined, regardless of the property value, all internal log4j2 logging will be 60 * printed to the console. The presence of this system property overrides any value set in the configuration's 61 * {@code <Configuration status="<level>" ...>} status attribute, as well as any value set for 62 * system property {@code org.apache.logging.log4j.simplelog.StatusLogger.level}. 63 * </p> 64 */ 65 public static final String LOG4J2_DEBUG = "log4j2.debug"; 66 67 private static int size(final String property, final int defaultValue) { 68 return PropertiesUtil.getProperties().getIntegerProperty(property, defaultValue); 69 } 70 71 /** 72 * Determines if a named Class can be loaded or not. 73 * 74 * @param className The class name. 75 * @return {@code true} if the class could be found or {@code false} otherwise. 76 */ 77 private static boolean isClassAvailable(final String className) { 78 try { 79 return LoaderUtil.loadClass(className) != null; 80 } catch (final Throwable e) { 81 return false; 82 } 83 } 84 85 /** 86 * Prevent class instantiation. 87 */ 88 private Constants() { 89 } 90 91 private static int getMajorVersion() { 92 return getMajorVersion(System.getProperty("java.version")); 93 } 94 95 static int getMajorVersion(final String version) { 96 final String[] parts = version.split("-|\\."); 97 boolean isJEP223; 98 try { 99 final int token = Integer.parseInt(parts[0]); 100 isJEP223 = token != 1; 101 if (isJEP223) { 102 return token; 103 } 104 return Integer.parseInt(parts[1]); 105 } catch (final Exception ex) { 106 return 0; 107 } 108 } 109 }