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.messaging.simp;
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.SimpLogging" to use
026 * for STOMP over WebSocket messaging when logging for
027 * "org.springframework.messaging.simp" is off but logging for
028 * "org.springframework.web" is on.
029 *
030 * <p>This makes it possible to enable all web related logging via
031 * "org.springframework.web" including logging from lower-level packages such as
032 * "org.springframework.messaging.simp".
033 *
034 * <p>To see logging from the primary classes where log messages originate from,
035 * simply enable logging for "org.springframework.messaging".
036 *
037 * @author Rossen Stoyanchev
038 * @since 5.1
039 * @see LogDelegateFactory
040 */
041public abstract class SimpLogging {
042
043        private static final Log fallbackLogger =
044                        LogFactory.getLog("org.springframework.web." + SimpLogging.class.getSimpleName());
045
046
047        /**
048         * Create a primary logger for the given class and wrap it with a composite
049         * that delegates to it or to the fallback logger named
050         * "org.springframework.web.SimpLogging", if the primary is not enabled.
051         * @param primaryLoggerClass the class for the name of the primary logger
052         * @return the resulting composite logger
053         */
054        public static Log forLogName(Class<?> primaryLoggerClass) {
055                Log primaryLogger = LogFactory.getLog(primaryLoggerClass);
056                return forLog(primaryLogger);
057        }
058
059        /**
060         * Wrap the given primary logger with a composite logger that delegates to
061         * either the primary or to the shared fallback logger
062         * "org.springframework.web.HttpLogging", if the primary is not enabled.
063         * @param primaryLogger the primary logger to use
064         * @return the resulting composite logger
065         */
066        public static Log forLog(Log primaryLogger) {
067                return LogDelegateFactory.getCompositeLog(primaryLogger, fallbackLogger);
068        }
069
070}