/* * Copyright 2002-2017 the original author or authors. * * Licensed 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. */
/** * A minimal incarnation of Apache Commons Logging's {@code LogFactory} API, * providing just the common {@link Log} lookup methods. This is inspired * by the JCL-over-SLF4J bridge and should be source as well as binary * compatible with all common use of the Commons Logging API (in particular: * with {@code LogFactory.getLog(Class/String)} field initializers). * * <p>This implementation does not support Commons Logging's original provider * detection. It rather only checks for the presence of the Log4j 2.x API * and the SLF4J 1.7 API in the Spring Framework classpath, falling back to * {@code java.util.logging} if none of the two is available. In that sense, * it works as a replacement for the Log4j 2 Commons Logging bridge as well as * the JCL-over-SLF4J bridge, both of which become irrelevant for Spring-based * setups as a consequence (with no need for manual excludes of the standard * Commons Logging API jar anymore either). Furthermore, for simple setups * without an external logging provider, Spring does not require any extra jar * on the classpath anymore since this embedded log factory automatically * delegates to {@code java.util.logging} in such a scenario. * * <p><b>Note that this Commons Logging variant is only meant to be used for * infrastructure logging purposes in the core framework and in extensions.</b> * It also serves as a common bridge for third-party libraries using the * Commons Logging API, e.g. Apache HttpClient, Castor and HtmlUnit, bringing * them into the same consistent arrangement without any extra bridge jars. * * <p><b>For logging need in application code, prefer direct use of Log4j 2.x * or SLF4J or {@code java.util.logging}.</b> Simply put Log4j 2.x or Logback * (or another SLF4J provider) onto your classpath, without any extra bridges, * and let the framework auto-adapt to your choice. * * @author Juergen Hoeller (for the {@code spring-jcl} variant) * @since 5.0 */ public abstract class LogFactory {
/** * Convenience method to return a named logger. * @param clazz containing Class from which a log name will be derived */ public static Log getLog(Class<?> clazz) { return getLog(clazz.getName()); }
/** * Convenience method to return a named logger. * @param name logical name of the <code>Log</code> instance to be returned */ public static Log getLog(String name) { switch (logApi) { case LOG4J: return Log4jDelegate.createLog(name); case SLF4J_LAL: return Slf4jDelegate.createLocationAwareLog(name); case SLF4J: return Slf4jDelegate.createLog(name); default: // Defensively use lazy-initializing delegate class here as well since the // java.logging module is not present by default on JDK 9. We are requiring // its presence if neither Log4j nor SLF4J is available; however, in the // case of Log4j or SLF4J, we are trying to prevent early initialization // of the JavaUtilLog adapter - e.g. by a JVM in debug mode - when eagerly // trying to parse the bytecode for all the cases of this switch clause. return JavaUtilDelegate.createLog(name); } }