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
033    /**
034     * Kill switch for object pooling in ThreadLocals that enables much of the LOG4J2-1270 no-GC behaviour.
035     * <p>
036     * {@code True} for non-{@link #IS_WEB_APP web apps}, disable by setting system property
037     * "log4j2.enable.threadlocals" to "false".
038     * </p>
039     */
040    public static final boolean ENABLE_THREADLOCALS = !IS_WEB_APP && PropertiesUtil.getProperties().getBooleanProperty(
041            "log4j2.enable.threadlocals", true);
042
043    public static final int JAVA_MAJOR_VERSION = getMajorVersion();
044
045    /**
046     * Maximum size of the StringBuilders used in RingBuffer LogEvents to store the contents of reusable Messages.
047     * After a large message has been delivered to the appenders, the StringBuilder is trimmed to this size.
048     * <p>
049     * The default value is {@value}, which allows the StringBuilder to resize three times from its initial size.
050     * Users can override with system property "log4j.maxReusableMsgSize".
051     * </p>
052     * @since 2.9
053     */
054    public static final int MAX_REUSABLE_MESSAGE_SIZE = size("log4j.maxReusableMsgSize", (128 * 2 + 2) * 2 + 2);
055
056    /**
057     * Name of the system property that will turn on TRACE level internal log4j2 status logging.
058     * <p>
059     * If system property {@value} is defined, regardless of the property value, all internal log4j2 logging will be
060     * printed to the console. The presence of this system property overrides any value set in the configuration's
061     * {@code <Configuration status="<level>" ...>} status attribute, as well as any value set for
062     * system property {@code org.apache.logging.log4j.simplelog.StatusLogger.level}.
063     * </p>
064     */
065    public static final String LOG4J2_DEBUG = "log4j2.debug";
066
067    private static int size(final String property, final int defaultValue) {
068        return PropertiesUtil.getProperties().getIntegerProperty(property, defaultValue);
069    }
070
071    /**
072     * Determines if a named Class can be loaded or not.
073     *
074     * @param className The class name.
075     * @return {@code true} if the class could be found or {@code false} otherwise.
076     */
077    private static boolean isClassAvailable(final String className) {
078        try {
079            return LoaderUtil.loadClass(className) != null;
080        } catch (final Throwable e) {
081            return false;
082        }
083    }
084
085    /**
086     * Prevent class instantiation.
087     */
088    private Constants() {
089    }
090
091    private static int getMajorVersion() {
092        final String version = System.getProperty("java.version");
093        final String[] parts = version.split("-|\\.");
094        boolean isJEP223;
095        try {
096            final int token = Integer.parseInt(parts[0]);
097            isJEP223 = token != 1;
098            if (isJEP223) {
099                return token;
100            }
101            return Integer.parseInt(parts[1]);
102        } catch (final Exception ex) {
103            return 0;
104        }
105    }
106}