001/*
002 * Copyright 2002-2019 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.apache.commons.logging;
018
019/**
020 * A minimal incarnation of Apache Commons Logging's {@code LogFactory} API,
021 * providing just the common {@link Log} lookup methods. This is inspired
022 * by the JCL-over-SLF4J bridge and should be source as well as binary
023 * compatible with all common use of the Commons Logging API (in particular:
024 * with {@code LogFactory.getLog(Class/String)} field initializers).
025 *
026 * <p>This implementation does not support Commons Logging's original provider
027 * detection. It rather only checks for the presence of the Log4j 2.x API
028 * and the SLF4J 1.7 API in the Spring Framework classpath, falling back to
029 * {@code java.util.logging} if none of the two is available. In that sense,
030 * it works as a replacement for the Log4j 2 Commons Logging bridge as well as
031 * the JCL-over-SLF4J bridge, both of which become irrelevant for Spring-based
032 * setups as a consequence (with no need for manual excludes of the standard
033 * Commons Logging API jar anymore either). Furthermore, for simple setups
034 * without an external logging provider, Spring does not require any extra jar
035 * on the classpath anymore since this embedded log factory automatically
036 * delegates to {@code java.util.logging} in such a scenario.
037 *
038 * <p><b>Note that this Commons Logging variant is only meant to be used for
039 * infrastructure logging purposes in the core framework and in extensions.</b>
040 * It also serves as a common bridge for third-party libraries using the
041 * Commons Logging API, e.g. Apache HttpClient, and HtmlUnit, bringing
042 * them into the same consistent arrangement without any extra bridge jars.
043 *
044 * <p><b>For logging need in application code, prefer direct use of Log4j 2.x
045 * or SLF4J or {@code java.util.logging}.</b> Simply put Log4j 2.x or Logback
046 * (or another SLF4J provider) onto your classpath, without any extra bridges,
047 * and let the framework auto-adapt to your choice.
048 *
049 * @author Juergen Hoeller (for the {@code spring-jcl} variant)
050 * @since 5.0
051 */
052public abstract class LogFactory {
053
054        /**
055         * Convenience method to return a named logger.
056         * @param clazz containing Class from which a log name will be derived
057         */
058        public static Log getLog(Class<?> clazz) {
059                return getLog(clazz.getName());
060        }
061
062        /**
063         * Convenience method to return a named logger.
064         * @param name logical name of the <code>Log</code> instance to be returned
065         */
066        public static Log getLog(String name) {
067                return LogAdapter.createLog(name);
068        }
069
070
071        /**
072         * This method only exists for compatibility with unusual Commons Logging API
073         * usage like e.g. {@code LogFactory.getFactory().getInstance(Class/String)}.
074         * @see #getInstance(Class)
075         * @see #getInstance(String)
076         * @deprecated in favor of {@link #getLog(Class)}/{@link #getLog(String)}
077         */
078        @Deprecated
079        public static LogFactory getFactory() {
080                return new LogFactory() {};
081        }
082
083        /**
084         * Convenience method to return a named logger.
085         * <p>This variant just dispatches straight to {@link #getLog(Class)}.
086         * @param clazz containing Class from which a log name will be derived
087         * @deprecated in favor of {@link #getLog(Class)}
088         */
089        @Deprecated
090        public Log getInstance(Class<?> clazz) {
091                return getLog(clazz);
092        }
093
094        /**
095         * Convenience method to return a named logger.
096         * <p>This variant just dispatches straight to {@link #getLog(String)}.
097         * @param name logical name of the <code>Log</code> instance to be returned
098         * @deprecated in favor of {@link #getLog(String)}
099         */
100        @Deprecated
101        public Log getInstance(String name) {
102                return getLog(name);
103        }
104
105}