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 || isClassAvailable("jakarta.servlet.Servlet"));
33
34 /**
35 * Kill switch for object pooling in ThreadLocals that enables much of the LOG4J2-1270 no-GC behaviour.
36 * <p>
37 * {@code True} for non-{@link #IS_WEB_APP web apps}, disable by setting system property
38 * "log4j2.enable.threadlocals" to "false".
39 * </p>
40 */
41 public static final boolean ENABLE_THREADLOCALS = !IS_WEB_APP && PropertiesUtil.getProperties().getBooleanProperty(
42 "log4j2.enable.threadlocals", true);
43
44 public static final int JAVA_MAJOR_VERSION = getMajorVersion();
45
46 /**
47 * Maximum size of the StringBuilders used in RingBuffer LogEvents to store the contents of reusable Messages.
48 * After a large message has been delivered to the appenders, the StringBuilder is trimmed to this size.
49 * <p>
50 * The default value is {@value}, which allows the StringBuilder to resize three times from its initial size.
51 * Users can override with system property "log4j.maxReusableMsgSize".
52 * </p>
53 * @since 2.9
54 */
55 public static final int MAX_REUSABLE_MESSAGE_SIZE = size("log4j.maxReusableMsgSize", (128 * 2 + 2) * 2 + 2);
56
57 /**
58 * Name of the system property that will turn on TRACE level internal log4j2 status logging.
59 * <p>
60 * If system property {@value} is either defined empty or its value equals to {@code true} (ignoring case), all internal log4j2 logging will be
61 * printed to the console. The presence of this system property overrides any value set in the configuration's
62 * {@code <Configuration status="<level>" ...>} status attribute, as well as any value set for
63 * system property {@code org.apache.logging.log4j.simplelog.StatusLogger.level}.
64 * </p>
65 */
66 public static final String LOG4J2_DEBUG = "log4j2.debug";
67
68 private static int size(final String property, final int defaultValue) {
69 return PropertiesUtil.getProperties().getIntegerProperty(property, defaultValue);
70 }
71
72 /**
73 * Determines if a named Class can be loaded or not.
74 *
75 * @param className The class name.
76 * @return {@code true} if the class could be found or {@code false} otherwise.
77 */
78 private static boolean isClassAvailable(final String className) {
79 try {
80 return LoaderUtil.loadClass(className) != null;
81 } catch (final Throwable e) {
82 return false;
83 }
84 }
85
86 /**
87 * The empty array.
88 */
89 public static final Object[] EMPTY_OBJECT_ARRAY = {};
90
91 /**
92 * The empty array.
93 */
94 public static final byte[] EMPTY_BYTE_ARRAY = {};
95
96 /**
97 * Prevent class instantiation.
98 */
99 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 }