Add LogUtils and HttpLogging

SPR-17012
This commit is contained in:
Rossen Stoyanchev
2018-07-18 09:47:50 -04:00
parent 23d4862017
commit 4d6f2df3cb
22 changed files with 212 additions and 112 deletions

View File

@@ -0,0 +1,158 @@
/*
* 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.List;
import java.util.function.Predicate;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.impl.NoOpLog;
/**
* Implementation of {@link Log} that wraps a list of loggers and delegates to
* the first one for which logging is enabled at the given level.
*
* @author Rossen Stoyanchev
* @since 5.1
* @see LogUtils#getCompositeLog
*/
class CompositeLog implements Log {
private static final Log noOpLog = new NoOpLog();
private final Log fatalLogger;
private final Log errorLogger;
private final Log warnLogger;
private final Log infoLogger;
private final Log debugLogger;
private final Log traceLogger;
/**
* Constructor with list of loggers. For optimal performance, the constructor
* checks and remembers which logger is on for each log category.
* @param loggers the loggers to use
*/
public CompositeLog(List<Log> loggers) {
this.fatalLogger = initLogger(loggers, Log::isFatalEnabled);
this.errorLogger = initLogger(loggers, Log::isErrorEnabled);
this.warnLogger = initLogger(loggers, Log::isWarnEnabled);
this.infoLogger = initLogger(loggers, Log::isInfoEnabled);
this.debugLogger = initLogger(loggers, Log::isDebugEnabled);
this.traceLogger = initLogger(loggers, Log::isTraceEnabled);
}
private static Log initLogger(List<Log> loggers, Predicate<Log> predicate) {
return loggers.stream().filter(predicate).findFirst().orElse(noOpLog);
}
@Override
public boolean isFatalEnabled() {
return this.fatalLogger != noOpLog;
}
@Override
public boolean isErrorEnabled() {
return this.errorLogger != noOpLog;
}
@Override
public boolean isWarnEnabled() {
return this.warnLogger != noOpLog;
}
@Override
public boolean isInfoEnabled() {
return this.infoLogger != noOpLog;
}
@Override
public boolean isDebugEnabled() {
return this.debugLogger != noOpLog;
}
@Override
public boolean isTraceEnabled() {
return this.traceLogger != noOpLog;
}
@Override
public void fatal(Object message) {
this.fatalLogger.fatal(message);
}
@Override
public void fatal(Object message, Throwable ex) {
this.fatalLogger.fatal(message, ex);
}
@Override
public void error(Object message) {
this.errorLogger.error(message);
}
@Override
public void error(Object message, Throwable ex) {
this.errorLogger.error(message);
}
@Override
public void warn(Object message) {
this.warnLogger.warn(message);
}
@Override
public void warn(Object message, Throwable ex) {
this.warnLogger.warn(message, ex);
}
@Override
public void info(Object message) {
this.infoLogger.info(message);
}
@Override
public void info(Object message, Throwable ex) {
this.infoLogger.info(message, ex);
}
@Override
public void debug(Object message) {
this.debugLogger.debug(message);
}
@Override
public void debug(Object message, Throwable ex) {
this.debugLogger.debug(message, ex);
}
@Override
public void trace(Object message) {
this.traceLogger.trace(message);
}
@Override
public void trace(Object message, Throwable ex) {
this.traceLogger.trace(message, ex);
}
}

View File

@@ -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());
}
}

View File

@@ -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;