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.springframework.core.log;
018
019import java.util.ArrayList;
020import java.util.Collections;
021import java.util.List;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025
026/**
027 * Factory for common {@link Log} delegates with Spring's logging conventions.
028 *
029 * <p>Mainly for internal use within the framework with Apache Commons Logging,
030 * typically in the form of the {@code spring-jcl} bridge but also compatible
031 * with other Commons Logging bridges.
032 *
033 * @author Rossen Stoyanchev
034 * @author Juergen Hoeller
035 * @since 5.1
036 * @see org.apache.commons.logging.LogFactory
037 */
038public final class LogDelegateFactory {
039
040        private LogDelegateFactory() {
041        }
042
043
044        /**
045         * Create a composite logger that delegates to a primary or falls back on a
046         * secondary logger if logging for the primary logger is not enabled.
047         * <p>This may be used for fallback logging from lower-level packages that
048         * logically should log together with some higher-level package but the two
049         * don't happen to share a suitable parent package (e.g. logging for the web
050         * and lower-level http and codec packages). For such cases the primary
051         * (class-based) logger can be wrapped with a shared fallback logger.
052         * @param primaryLogger primary logger to try first
053         * @param secondaryLogger secondary logger
054         * @param tertiaryLoggers optional vararg of further fallback loggers
055         * @return the resulting composite logger for the related categories
056         */
057        public static Log getCompositeLog(Log primaryLogger, Log secondaryLogger, Log... tertiaryLoggers) {
058                List<Log> loggers = new ArrayList<>(2 + tertiaryLoggers.length);
059                loggers.add(primaryLogger);
060                loggers.add(secondaryLogger);
061                Collections.addAll(loggers, tertiaryLoggers);
062                return new CompositeLog(loggers);
063        }
064
065        /**
066         * Create a "hidden" logger whose name is intentionally prefixed with "_"
067         * because its output is either too verbose or otherwise deemed as optional
068         * or unnecessary to see at any log level by default under the normal package
069         * based log hierarchy.
070         * @param clazz the class for which to create a logger
071         * @return a logger for the hidden category ("_" + fully-qualified class name)
072         */
073        public static Log getHiddenLog(Class<?> clazz) {
074                return LogFactory.getLog("_" + clazz.getName());
075        }
076
077}