001/*
002 * Copyright 2002-2018 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.http;
018
019import org.apache.commons.logging.Log;
020import org.apache.commons.logging.LogFactory;
021
022import org.springframework.core.log.LogDelegateFactory;
023
024/**
025 * Holds the shared logger named "org.springframework.web.HttpLogging" for HTTP
026 * related logging when "org.springframework.http" is not enabled but
027 * "org.springframework.web" is.
028 *
029 * <p>That means "org.springframework.web" enables all web logging including
030 * from lower level packages such as "org.springframework.http" and modules
031 * such as codecs from {@literal "spring-core"} when those are wrapped with
032 * {@link org.springframework.http.codec.EncoderHttpMessageWriter EncoderHttpMessageWriter} or
033 * {@link org.springframework.http.codec.DecoderHttpMessageReader DecoderHttpMessageReader}.
034 *
035 * <p>To see logging from the primary class loggers simply enable logging for
036 * "org.springframework.http" and "org.springframework.codec".
037 *
038 * @author Rossen Stoyanchev
039 * @since 5.1
040 * @see LogDelegateFactory
041 */
042public abstract class HttpLogging {
043
044        private static final Log fallbackLogger =
045                        LogFactory.getLog("org.springframework.web." + HttpLogging.class.getSimpleName());
046
047
048        /**
049         * Create a primary logger for the given class and wrap it with a composite
050         * that delegates to it or to the fallback logger
051         * "org.springframework.web.HttpLogging", if the primary is not enabled.
052         * @param primaryLoggerClass the class for the name of the primary logger
053         * @return the resulting composite logger
054         */
055        public static Log forLogName(Class<?> primaryLoggerClass) {
056                Log primaryLogger = LogFactory.getLog(primaryLoggerClass);
057                return forLog(primaryLogger);
058        }
059
060        /**
061         * Wrap the given primary logger with a composite logger that delegates to
062         * it or to the fallback logger "org.springframework.web.HttpLogging",
063         * if the primary is not enabled.
064         * @param primaryLogger the primary logger to use
065         * @return the resulting composite logger
066         */
067        public static Log forLog(Log primaryLogger) {
068                return LogDelegateFactory.getCompositeLog(primaryLogger, fallbackLogger);
069        }
070
071}