Fallback logger for logging in http and codec packages
Issue: SPR-17012
This commit is contained in:
parent
bca9f51092
commit
4bd22eeb13
|
|
@ -40,7 +40,7 @@ import org.springframework.util.MimeType;
|
|||
*/
|
||||
public abstract class AbstractDecoder<T> implements Decoder<T> {
|
||||
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
protected Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private final List<MimeType> decodableMimeTypes;
|
||||
|
||||
|
|
@ -50,6 +50,24 @@ public abstract class AbstractDecoder<T> implements Decoder<T> {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set an alternative logger to use than the one based on the class name.
|
||||
* @param logger the logger to use
|
||||
* @since 5.1
|
||||
*/
|
||||
public void setLogger(Log logger) {
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the currently configured Logger.
|
||||
* @since 5.1
|
||||
*/
|
||||
public Log getLogger() {
|
||||
return logger;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<MimeType> getDecodableMimeTypes() {
|
||||
return this.decodableMimeTypes;
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ import org.springframework.util.MimeType;
|
|||
*/
|
||||
public abstract class AbstractEncoder<T> implements Encoder<T> {
|
||||
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
protected Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private final List<MimeType> encodableMimeTypes;
|
||||
|
||||
|
|
@ -46,6 +46,24 @@ public abstract class AbstractEncoder<T> implements Encoder<T> {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set an alternative logger to use than the one based on the class name.
|
||||
* @param logger the logger to use
|
||||
* @since 5.1
|
||||
*/
|
||||
public void setLogger(Log logger) {
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the currently configured Logger.
|
||||
* @since 5.1
|
||||
*/
|
||||
public Log getLogger() {
|
||||
return logger;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<MimeType> getEncodableMimeTypes() {
|
||||
return this.encodableMimeTypes;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,195 @@
|
|||
/*
|
||||
* 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 java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.commons.logging.impl.NoOpLog;
|
||||
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* Composite {@link Log} configured with a primary logger and a list of secondary
|
||||
* ones to fall back on if the main one is not enabled.
|
||||
*
|
||||
* <p>This class also declares {@link #webLogger} for use as fallback when
|
||||
* logging in the "org.springframework.http" package.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 5.1
|
||||
*/
|
||||
public final class HttpLog implements Log {
|
||||
|
||||
/**
|
||||
* Logger with category "org.springframework.web.HTTP" to use as fallback
|
||||
* if "org.springframework.web" is on.
|
||||
*/
|
||||
public static final Log webLogger = LogFactory.getLog("org.springframework.web.HTTP");
|
||||
|
||||
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;
|
||||
|
||||
|
||||
private HttpLog(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);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a composite logger that uses the given primary logger, if enabled,
|
||||
* or falls back on {@link #webLogger}.
|
||||
* @param primaryLogger the primary logger
|
||||
* @return a composite logger
|
||||
*/
|
||||
public static Log create(Log primaryLogger) {
|
||||
return createWith(primaryLogger, webLogger);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a composite logger that uses the given primary logger, if enabled,
|
||||
* or falls back on one of the given secondary loggers..
|
||||
* @param primaryLogger the primary logger
|
||||
* @param secondaryLoggers fallback loggers
|
||||
* @return a composite logger
|
||||
*/
|
||||
public static Log createWith(Log primaryLogger, Log... secondaryLoggers) {
|
||||
if (ObjectUtils.isEmpty(secondaryLoggers)) {
|
||||
return primaryLogger;
|
||||
}
|
||||
List<Log> loggers = new ArrayList<>(1 + secondaryLoggers.length);
|
||||
loggers.add(primaryLogger);
|
||||
Collections.addAll(loggers, secondaryLoggers);
|
||||
return new HttpLog(loggers);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -19,12 +19,15 @@ package org.springframework.http.codec;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.codec.AbstractDecoder;
|
||||
import org.springframework.core.codec.Decoder;
|
||||
import org.springframework.core.codec.Hints;
|
||||
import org.springframework.http.HttpLog;
|
||||
import org.springframework.http.HttpMessage;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ReactiveHttpInputMessage;
|
||||
|
|
@ -60,6 +63,16 @@ public class DecoderHttpMessageReader<T> implements HttpMessageReader<T> {
|
|||
Assert.notNull(decoder, "Decoder is required");
|
||||
this.decoder = decoder;
|
||||
this.mediaTypes = MediaType.asMediaTypes(decoder.getDecodableMimeTypes());
|
||||
initLogger(decoder);
|
||||
}
|
||||
|
||||
private void initLogger(Decoder<T> decoder) {
|
||||
if (decoder instanceof AbstractDecoder &&
|
||||
decoder.getClass().getPackage().getName().startsWith("org.springframework.core.codec")) {
|
||||
|
||||
Log logger = HttpLog.create(((AbstractDecoder) decoder).getLogger());
|
||||
((AbstractDecoder) decoder).setLogger(logger);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -19,15 +19,18 @@ package org.springframework.http.codec;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.codec.AbstractEncoder;
|
||||
import org.springframework.core.codec.Encoder;
|
||||
import org.springframework.core.codec.Hints;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpLog;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ReactiveHttpOutputMessage;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
|
|
@ -66,6 +69,16 @@ public class EncoderHttpMessageWriter<T> implements HttpMessageWriter<T> {
|
|||
this.encoder = encoder;
|
||||
this.mediaTypes = MediaType.asMediaTypes(encoder.getEncodableMimeTypes());
|
||||
this.defaultMediaType = initDefaultMediaType(this.mediaTypes);
|
||||
initLogger(encoder);
|
||||
}
|
||||
|
||||
private void initLogger(Encoder<T> encoder) {
|
||||
if (encoder instanceof AbstractEncoder &&
|
||||
encoder.getClass().getPackage().getName().startsWith("org.springframework.core.codec")) {
|
||||
|
||||
Log logger = HttpLog.create(((AbstractEncoder) encoder).getLogger());
|
||||
((AbstractEncoder) encoder).setLogger(logger);
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
|
|
|||
|
|
@ -18,6 +18,8 @@ package org.springframework.http.codec;
|
|||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.http.HttpLog;
|
||||
|
||||
/**
|
||||
* Base class for {@link org.springframework.core.codec.Encoder},
|
||||
* {@link org.springframework.core.codec.Decoder}, {@link HttpMessageReader}, or
|
||||
|
|
@ -29,7 +31,7 @@ import org.apache.commons.logging.LogFactory;
|
|||
*/
|
||||
public class LoggingCodecSupport {
|
||||
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
protected final Log logger = HttpLog.create(LogFactory.getLog(getClass()));
|
||||
|
||||
/** Do not log potentially sensitive information (params at DEBUG and headers at TRACE). */
|
||||
private boolean disableLoggingRequestDetails = false;
|
||||
|
|
|
|||
|
|
@ -39,6 +39,7 @@ import org.springframework.core.io.buffer.DataBuffer;
|
|||
import org.springframework.core.io.buffer.DataBufferFactory;
|
||||
import org.springframework.core.io.support.ResourceRegion;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpLog;
|
||||
import org.springframework.http.HttpRange;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
|
|
@ -72,7 +73,7 @@ public class ResourceHttpMessageWriter implements HttpMessageWriter<Resource> {
|
|||
|
||||
private static final ResolvableType REGION_TYPE = ResolvableType.forClass(ResourceRegion.class);
|
||||
|
||||
private static final Log logger = LogFactory.getLog(ResourceHttpMessageWriter.class);
|
||||
private static final Log logger = HttpLog.create(LogFactory.getLog(ResourceHttpMessageWriter.class));
|
||||
|
||||
|
||||
private final ResourceEncoder encoder;
|
||||
|
|
|
|||
|
|
@ -35,6 +35,7 @@ import org.springframework.core.GenericTypeResolver;
|
|||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.codec.Hints;
|
||||
import org.springframework.http.HttpLog;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.MimeType;
|
||||
|
|
@ -65,7 +66,7 @@ public abstract class Jackson2CodecSupport {
|
|||
new MimeType("application", "*+json", StandardCharsets.UTF_8)));
|
||||
|
||||
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
protected final Log logger = HttpLog.create(LogFactory.getLog(getClass()));
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@ import org.apache.commons.logging.LogFactory;
|
|||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpInputMessage;
|
||||
import org.springframework.http.HttpLog;
|
||||
import org.springframework.http.HttpOutputMessage;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.StreamingHttpOutputMessage;
|
||||
|
|
@ -51,7 +52,7 @@ import org.springframework.util.Assert;
|
|||
public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConverter<T> {
|
||||
|
||||
/** Logger available to subclasses. */
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
protected final Log logger = HttpLog.create(LogFactory.getLog(getClass()));
|
||||
|
||||
private List<MediaType> supportedMediaTypes = Collections.emptyList();
|
||||
|
||||
|
|
|
|||
|
|
@ -60,6 +60,7 @@ import org.springframework.beans.BeanUtils;
|
|||
import org.springframework.beans.FatalBeanException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.KotlinDetector;
|
||||
import org.springframework.http.HttpLog;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
|
@ -103,7 +104,7 @@ public class Jackson2ObjectMapperBuilder {
|
|||
|
||||
private static volatile boolean kotlinWarningLogged = false;
|
||||
|
||||
private final Log logger = LogFactory.getLog(getClass());
|
||||
private final Log logger = HttpLog.create(LogFactory.getLog(getClass()));
|
||||
|
||||
private final Map<Class<?>, Class<?>> mixIns = new HashMap<>();
|
||||
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ import org.apache.commons.logging.LogFactory;
|
|||
|
||||
import org.springframework.http.HttpCookie;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpLog;
|
||||
import org.springframework.http.server.RequestPath;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
|
@ -43,7 +44,7 @@ import org.springframework.util.StringUtils;
|
|||
*/
|
||||
public abstract class AbstractServerHttpRequest implements ServerHttpRequest {
|
||||
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
protected final Log logger = HttpLog.create(LogFactory.getLog(getClass()));
|
||||
|
||||
private static final Pattern QUERY_PATTERN = Pattern.compile("([^&=]+)(=?)([^&]+)?");
|
||||
|
||||
|
|
|
|||
|
|
@ -31,6 +31,7 @@ import reactor.core.publisher.Mono;
|
|||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DataBufferFactory;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpLog;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseCookie;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
|
@ -57,7 +58,7 @@ public abstract class AbstractServerHttpResponse implements ServerHttpResponse {
|
|||
*/
|
||||
private enum State {NEW, COMMITTING, COMMITTED}
|
||||
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
protected final Log logger = HttpLog.create(LogFactory.getLog(getClass()));
|
||||
|
||||
|
||||
private final DataBufferFactory dataBufferFactory;
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ import reactor.netty.http.server.HttpServerRequest;
|
|||
import reactor.netty.http.server.HttpServerResponse;
|
||||
|
||||
import org.springframework.core.io.buffer.NettyDataBufferFactory;
|
||||
import org.springframework.http.HttpLog;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
|
|
@ -39,7 +40,7 @@ import org.springframework.util.Assert;
|
|||
*/
|
||||
public class ReactorHttpHandlerAdapter implements BiFunction<HttpServerRequest, HttpServerResponse, Mono<Void>> {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(ReactorHttpHandlerAdapter.class);
|
||||
private static final Log logger = HttpLog.create(LogFactory.getLog(ReactorHttpHandlerAdapter.class));
|
||||
|
||||
|
||||
private final HttpHandler httpHandler;
|
||||
|
|
|
|||
|
|
@ -41,6 +41,7 @@ import org.reactivestreams.Subscription;
|
|||
|
||||
import org.springframework.core.io.buffer.DataBufferFactory;
|
||||
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
||||
import org.springframework.http.HttpLog;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
|
@ -57,7 +58,7 @@ import org.springframework.util.Assert;
|
|||
@SuppressWarnings("serial")
|
||||
public class ServletHttpHandlerAdapter implements Servlet {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(ServletHttpHandlerAdapter.class);
|
||||
private static final Log logger = HttpLog.create(LogFactory.getLog(ServletHttpHandlerAdapter.class));
|
||||
|
||||
private static final int DEFAULT_BUFFER_SIZE = 8192;
|
||||
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ import org.reactivestreams.Subscription;
|
|||
|
||||
import org.springframework.core.io.buffer.DataBufferFactory;
|
||||
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
||||
import org.springframework.http.HttpLog;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
|
|
@ -40,7 +41,7 @@ import org.springframework.util.Assert;
|
|||
*/
|
||||
public class UndertowHttpHandlerAdapter implements io.undertow.server.HttpHandler {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(UndertowHttpHandlerAdapter.class);
|
||||
private static final Log logger = HttpLog.create(LogFactory.getLog(UndertowHttpHandlerAdapter.class));
|
||||
|
||||
|
||||
private final HttpHandler httpHandler;
|
||||
|
|
|
|||
Loading…
Reference in New Issue