22 changed files with 212 additions and 112 deletions
@ -0,0 +1,67 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2002-2018 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. |
||||||
|
*/ |
||||||
|
package org.springframework.util.log; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.Collections; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
import org.apache.commons.logging.Log; |
||||||
|
import org.apache.commons.logging.LogFactory; |
||||||
|
|
||||||
|
/** |
||||||
|
* Utilities to assist with logging. Mainly for internal use within the framework |
||||||
|
* with Appache Commons Logging. |
||||||
|
* |
||||||
|
* @author Rossen Stoyanchev |
||||||
|
* @since 5.1 |
||||||
|
*/ |
||||||
|
public abstract class LogUtils { |
||||||
|
|
||||||
|
/** |
||||||
|
* Create a composite logger that delegates to a primary or falls back on a |
||||||
|
* secondary logger if logging for the primary logger is not enabled. |
||||||
|
* <p>This may be used for fallback logging from lower level packages that |
||||||
|
* logically should log together with some higher level package but the two |
||||||
|
* don't happen to share a suitable parent package (e.g. logging for the web |
||||||
|
* and lower level http and codec packages). For such cases the primary, |
||||||
|
* class-based logger can be wrapped with a shared fallback logger. |
||||||
|
* @param primaryLogger primary logger to try first |
||||||
|
* @param secondaryLogger secondary logger |
||||||
|
* @param tertiaryLoggers optionally, more fallback loggers |
||||||
|
* @return the resulting logger to use |
||||||
|
*/ |
||||||
|
public static Log getCompositeLog(Log primaryLogger, Log secondaryLogger, Log... tertiaryLoggers) { |
||||||
|
List<Log> loggers = new ArrayList<>(2 + tertiaryLoggers.length); |
||||||
|
loggers.add(primaryLogger); |
||||||
|
loggers.add(secondaryLogger); |
||||||
|
Collections.addAll(loggers, tertiaryLoggers); |
||||||
|
return new CompositeLog(loggers); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Create a "hidden" logger whose name is intentionally prefixed with "_" |
||||||
|
* because its output is either too verbose or otherwise deemed as optional |
||||||
|
* or unnecessary to see at any log level by default under the normal package
|
||||||
|
* based log hierarchy. |
||||||
|
* @param clazz the class for which to create a logger |
||||||
|
* @return the created logger |
||||||
|
*/ |
||||||
|
public static Log getHiddenLog(Class<?> clazz) { |
||||||
|
return LogFactory.getLog("_" + clazz.getName()); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,9 @@ |
|||||||
|
/** |
||||||
|
* Useful helper classes for logging. |
||||||
|
*/ |
||||||
|
@NonNullApi |
||||||
|
@NonNullFields |
||||||
|
package org.springframework.util.log; |
||||||
|
|
||||||
|
import org.springframework.lang.NonNullApi; |
||||||
|
import org.springframework.lang.NonNullFields; |
||||||
@ -0,0 +1,69 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2002-2018 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. |
||||||
|
*/ |
||||||
|
package org.springframework.http; |
||||||
|
|
||||||
|
import org.apache.commons.logging.Log; |
||||||
|
import org.apache.commons.logging.LogFactory; |
||||||
|
|
||||||
|
import org.springframework.util.log.LogUtils; |
||||||
|
|
||||||
|
/** |
||||||
|
* Holds the shared logger named "org.springframework.web.HttpLogging" for HTTP |
||||||
|
* related logging when "org.springframework.http" is not enabled but |
||||||
|
* "org.springframework.web" is. |
||||||
|
* |
||||||
|
* <p>That means "org.springframework.web" enables all web logging including |
||||||
|
* from lower level packages such as "org.springframework.http" and modules |
||||||
|
* such as codecs from {@literal "spring-core"} when those are wrapped with |
||||||
|
* {@link org.springframework.http.codec.EncoderHttpMessageWriter EncoderHttpMessageWriter} or |
||||||
|
* {@link org.springframework.http.codec.DecoderHttpMessageReader DecoderHttpMessageReader}. |
||||||
|
* |
||||||
|
* <p>To see logging from the primary class loggers simply enable logging for |
||||||
|
* "org.springframework.http" and "org.springframework.codec". |
||||||
|
* |
||||||
|
* @author Rossen Stoyanchev |
||||||
|
* @since 5.1 |
||||||
|
*/ |
||||||
|
public abstract class HttpLogging { |
||||||
|
|
||||||
|
private static final Log fallbackLogger = |
||||||
|
LogFactory.getLog("org.springframework.web." + HttpLogging.class.getSimpleName()); |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Create a primary logger for the given class and wrap it with a composite |
||||||
|
* that delegates to it or to the fallback logger |
||||||
|
* "org.springframework.web.HttpLogging", if the primary is not enabled. |
||||||
|
* @param primaryLoggerClass the class for the name of the primary logger |
||||||
|
* @return the resulting composite logger |
||||||
|
*/ |
||||||
|
public static Log forLogName(Class<?> primaryLoggerClass) { |
||||||
|
Log primaryLogger = LogFactory.getLog(primaryLoggerClass); |
||||||
|
return forLog(primaryLogger); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Wrap the given primary logger with a composite logger that delegates to |
||||||
|
* it or to the fallback logger "org.springframework.web.HttpLogging", if |
||||||
|
* the primary is not enabled. |
||||||
|
* @param primaryLogger the primary logger to use |
||||||
|
* @return the resulting composite logger |
||||||
|
*/ |
||||||
|
public static Log forLog(Log primaryLogger) { |
||||||
|
return LogUtils.getCompositeLog(primaryLogger, fallbackLogger); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue