Repository: logging-log4j2 Updated Branches: refs/heads/master cfc5897c0 -> 83b930dcf
LOG4J2-1417 create Constants class in log4j-api module, used by AbstractLogger and by log4j-core Constants Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/c930cc2b Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/c930cc2b Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/c930cc2b Branch: refs/heads/master Commit: c930cc2b80cfbdbffb6b3971aa3cd23aa87f7e4f Parents: cfc5897 Author: rpopma <[email protected]> Authored: Sat Jun 11 17:10:49 2016 +0900 Committer: rpopma <[email protected]> Committed: Sat Jun 11 17:10:49 2016 +0900 ---------------------------------------------------------------------- .../logging/log4j/spi/AbstractLogger.java | 21 +------ .../apache/logging/log4j/util/Constants.java | 61 ++++++++++++++++++++ .../logging/log4j/core/util/Constants.java | 6 +- 3 files changed, 65 insertions(+), 23 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c930cc2b/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractLogger.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractLogger.java b/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractLogger.java index bb4886a..80ae730 100644 --- a/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractLogger.java +++ b/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractLogger.java @@ -31,6 +31,7 @@ import org.apache.logging.log4j.message.ParameterizedMessageFactory; import org.apache.logging.log4j.message.ReusableMessageFactory; import org.apache.logging.log4j.message.StringFormattedMessage; import org.apache.logging.log4j.status.StatusLogger; +import org.apache.logging.log4j.util.Constants; import org.apache.logging.log4j.util.LambdaUtil; import org.apache.logging.log4j.util.LoaderUtil; import org.apache.logging.log4j.util.MessageSupplier; @@ -185,11 +186,7 @@ public abstract class AbstractLogger implements ExtendedLogger, Serializable { final Class<ReusableMessageFactory> reusableParameterizedMessageFactoryClass, final Class<ParameterizedMessageFactory> parameterizedMessageFactoryClass) { try { - final boolean IS_WEB_APP = PropertiesUtil.getProperties().getBooleanProperty( - "log4j2.is.webapp", isClassAvailable("javax.servlet.Servlet")); - final boolean ENABLE_THREADLOCALS = !IS_WEB_APP && PropertiesUtil.getProperties().getBooleanProperty( - "log4j2.enable.threadlocals", true); - final String fallback = ENABLE_THREADLOCALS ? reusableParameterizedMessageFactoryClass.getName() + final String fallback = Constants.ENABLE_THREADLOCALS ? reusableParameterizedMessageFactoryClass.getName() : parameterizedMessageFactoryClass.getName(); final String clsName = PropertiesUtil.getProperties().getStringProperty(property, fallback); return LoaderUtil.loadClass(clsName).asSubclass(MessageFactory.class); @@ -198,20 +195,6 @@ public abstract class AbstractLogger implements ExtendedLogger, Serializable { } } - /** - * Determines if a named Class can be loaded or not. - * - * @param className The class name. - * @return {@code true} if the class could be found or {@code false} otherwise. - */ - private static boolean isClassAvailable(final String className) { - try { - return LoaderUtil.loadClass(className) != null; - } catch (final Throwable e) { - return false; - } - } - private static Class<? extends FlowMessageFactory> createFlowClassForProperty(final String property, final Class<DefaultFlowMessageFactory> defaultFlowMessageFactoryClass) { try { http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c930cc2b/log4j-api/src/main/java/org/apache/logging/log4j/util/Constants.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util/Constants.java b/log4j-api/src/main/java/org/apache/logging/log4j/util/Constants.java new file mode 100644 index 0000000..2b64550 --- /dev/null +++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/Constants.java @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ +package org.apache.logging.log4j.util; + +/** + * Log4j API Constants. + * + * @since 2.6.2 + */ +public final class Constants { + /** + * {@code true} if we think we are running in a web container, based on the boolean value of system property + * "log4j2.is.webapp", or (if this system property is not set) whether the {@code javax.servlet.Servlet} class + * is present in the classpath. + */ + public static final boolean IS_WEB_APP = PropertiesUtil.getProperties().getBooleanProperty( + "log4j2.is.webapp", isClassAvailable("javax.servlet.Servlet")); + + /** + * Kill switch for object pooling in ThreadLocals that enables much of the LOG4J2-1270 no-GC behaviour. + * <p> + * {@code True} for non-{@link #IS_WEB_APP web apps}, disable by setting system property + * "log4j2.enable.threadlocals" to "false". + */ + public static final boolean ENABLE_THREADLOCALS = !IS_WEB_APP && PropertiesUtil.getProperties().getBooleanProperty( + "log4j2.enable.threadlocals", true); + + /** + * Determines if a named Class can be loaded or not. + * + * @param className The class name. + * @return {@code true} if the class could be found or {@code false} otherwise. + */ + private static boolean isClassAvailable(final String className) { + try { + return LoaderUtil.loadClass(className) != null; + } catch (final Throwable e) { + return false; + } + } + + /** + * Prevent class instantiation. + */ + private Constants() { + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c930cc2b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Constants.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Constants.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Constants.java index ef9764a..09ec0bb 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Constants.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Constants.java @@ -76,8 +76,7 @@ public final class Constants { * "log4j2.is.webapp", or (if this system property is not set) whether the {@code javax.servlet.Servlet} class * is present in the classpath. */ - public static final boolean IS_WEB_APP = PropertiesUtil.getProperties().getBooleanProperty( - "log4j2.is.webapp", Loader.isClassAvailable("javax.servlet.Servlet")); + public static final boolean IS_WEB_APP = org.apache.logging.log4j.util.Constants.IS_WEB_APP; /** * Kill switch for object pooling in ThreadLocals that enables much of the LOG4J2-1270 no-GC behaviour. @@ -87,8 +86,7 @@ public final class Constants { * * @since 2.6 */ - public static final boolean ENABLE_THREADLOCALS = !IS_WEB_APP && PropertiesUtil.getProperties().getBooleanProperty( - "log4j2.enable.threadlocals", true); + public static final boolean ENABLE_THREADLOCALS = org.apache.logging.log4j.util.Constants.ENABLE_THREADLOCALS; /** * Kill switch for garbage-free Layout behaviour that encodes LogEvents directly into
