001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017package org.apache.logging.log4j.util; 018 019/** 020 * Log4j API Constants. 021 * 022 * @since 2.6.2 023 */ 024public final class Constants { 025 /** 026 * {@code true} if we think we are running in a web container, based on the boolean value of system property 027 * "log4j2.is.webapp", or (if this system property is not set) whether the {@code javax.servlet.Servlet} class 028 * is present in the classpath. 029 */ 030 public static final boolean IS_WEB_APP = PropertiesUtil.getProperties().getBooleanProperty( 031 "log4j2.is.webapp", isClassAvailable("javax.servlet.Servlet") 032 || isClassAvailable("jakarta.servlet.Servlet")); 033 034 /** 035 * Kill switch for object pooling in ThreadLocals that enables much of the LOG4J2-1270 no-GC behaviour. 036 * <p> 037 * {@code True} for non-{@link #IS_WEB_APP web apps}, disable by setting system property 038 * "log4j2.enable.threadlocals" to "false". 039 * </p> 040 */ 041 public static final boolean ENABLE_THREADLOCALS = !IS_WEB_APP && PropertiesUtil.getProperties().getBooleanProperty( 042 "log4j2.enable.threadlocals", true); 043 044 public static final int JAVA_MAJOR_VERSION = getMajorVersion(); 045 046 /** 047 * Maximum size of the StringBuilders used in RingBuffer LogEvents to store the contents of reusable Messages. 048 * After a large message has been delivered to the appenders, the StringBuilder is trimmed to this size. 049 * <p> 050 * The default value is {@value}, which allows the StringBuilder to resize three times from its initial size. 051 * Users can override with system property "log4j.maxReusableMsgSize". 052 * </p> 053 * @since 2.9 054 */ 055 public static final int MAX_REUSABLE_MESSAGE_SIZE = size("log4j.maxReusableMsgSize", (128 * 2 + 2) * 2 + 2); 056 057 /** 058 * Name of the system property that will turn on TRACE level internal log4j2 status logging. 059 * <p> 060 * If system property {@value} is either defined empty or its value equals to {@code true} (ignoring case), all internal log4j2 logging will be 061 * printed to the console. The presence of this system property overrides any value set in the configuration's 062 * {@code <Configuration status="<level>" ...>} status attribute, as well as any value set for 063 * system property {@code org.apache.logging.log4j.simplelog.StatusLogger.level}. 064 * </p> 065 */ 066 public static final String LOG4J2_DEBUG = "log4j2.debug"; 067 068 private static int size(final String property, final int defaultValue) { 069 return PropertiesUtil.getProperties().getIntegerProperty(property, defaultValue); 070 } 071 072 /** 073 * Determines if a named Class can be loaded or not. 074 * 075 * @param className The class name. 076 * @return {@code true} if the class could be found or {@code false} otherwise. 077 */ 078 private static boolean isClassAvailable(final String className) { 079 try { 080 return LoaderUtil.loadClass(className) != null; 081 } catch (final Throwable e) { 082 return false; 083 } 084 } 085 086 /** 087 * The empty array. 088 */ 089 public static final Object[] EMPTY_OBJECT_ARRAY = {}; 090 091 /** 092 * The empty array. 093 */ 094 public static final byte[] EMPTY_BYTE_ARRAY = {}; 095 096 /** 097 * Prevent class instantiation. 098 */ 099 private Constants() { 100 } 101 102 private static int getMajorVersion() { 103 return getMajorVersion(System.getProperty("java.version")); 104 } 105 106 static int getMajorVersion(final String version) { 107 final String[] parts = version.split("-|\\."); 108 boolean isJEP223; 109 try { 110 final int token = Integer.parseInt(parts[0]); 111 isJEP223 = token != 1; 112 if (isJEP223) { 113 return token; 114 } 115 return Integer.parseInt(parts[1]); 116 } catch (final Exception ex) { 117 return 0; 118 } 119 } 120}