diff --git a/spring-core/src/main/java/org/springframework/core/codec/ByteArrayEncoder.java b/spring-core/src/main/java/org/springframework/core/codec/ByteArrayEncoder.java index 433b71ae02..782fedfc0e 100644 --- a/spring-core/src/main/java/org/springframework/core/codec/ByteArrayEncoder.java +++ b/spring-core/src/main/java/org/springframework/core/codec/ByteArrayEncoder.java @@ -18,7 +18,6 @@ package org.springframework.core.codec; import java.util.Map; -import org.apache.commons.logging.Log; import org.reactivestreams.Publisher; import reactor.core.publisher.Flux; @@ -55,10 +54,9 @@ public class ByteArrayEncoder extends AbstractEncoder { return Flux.from(inputStream).map(bytes -> { DataBuffer dataBuffer = bufferFactory.wrap(bytes); - Log theLogger = Hints.getLoggerOrDefault(hints, logger); - if (theLogger.isDebugEnabled()) { - theLogger.debug(Hints.getLogPrefix(hints) + - "Writing " + dataBuffer.readableByteCount() + " bytes"); + if (logger.isDebugEnabled() && !Hints.suppressLogging(hints)) { + String logPrefix = Hints.getLogPrefix(hints); + logger.debug(logPrefix + "Writing " + dataBuffer.readableByteCount() + " bytes"); } return dataBuffer; }); diff --git a/spring-core/src/main/java/org/springframework/core/codec/ByteBufferEncoder.java b/spring-core/src/main/java/org/springframework/core/codec/ByteBufferEncoder.java index d5b02b7e82..81d3b7ff86 100644 --- a/spring-core/src/main/java/org/springframework/core/codec/ByteBufferEncoder.java +++ b/spring-core/src/main/java/org/springframework/core/codec/ByteBufferEncoder.java @@ -19,7 +19,6 @@ package org.springframework.core.codec; import java.nio.ByteBuffer; import java.util.Map; -import org.apache.commons.logging.Log; import org.reactivestreams.Publisher; import reactor.core.publisher.Flux; @@ -56,10 +55,9 @@ public class ByteBufferEncoder extends AbstractEncoder { return Flux.from(inputStream).map(byteBuffer -> { DataBuffer dataBuffer = bufferFactory.wrap(byteBuffer); - Log theLogger = Hints.getLoggerOrDefault(hints, logger); - if (theLogger.isDebugEnabled()) { - theLogger.debug(Hints.getLogPrefix(hints) + - "Writing " + dataBuffer.readableByteCount() + " bytes"); + if (logger.isDebugEnabled() && !Hints.suppressLogging(hints)) { + String logPrefix = Hints.getLogPrefix(hints); + logger.debug(logPrefix + "Writing " + dataBuffer.readableByteCount() + " bytes"); } return dataBuffer; }); diff --git a/spring-core/src/main/java/org/springframework/core/codec/CharSequenceEncoder.java b/spring-core/src/main/java/org/springframework/core/codec/CharSequenceEncoder.java index e59879b699..02170a343c 100644 --- a/spring-core/src/main/java/org/springframework/core/codec/CharSequenceEncoder.java +++ b/spring-core/src/main/java/org/springframework/core/codec/CharSequenceEncoder.java @@ -22,7 +22,6 @@ import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.Map; -import org.apache.commons.logging.Log; import org.reactivestreams.Publisher; import reactor.core.publisher.Flux; @@ -69,9 +68,9 @@ public final class CharSequenceEncoder extends AbstractEncoder { Charset charset = getCharset(mimeType); return Flux.from(inputStream).map(charSequence -> { - Log theLogger = Hints.getLoggerOrDefault(hints, logger); - if (theLogger.isDebugEnabled()) { - theLogger.debug(Hints.getLogPrefix(hints) + "Writing '" + charSequence + "'"); + if (logger.isDebugEnabled() && !Hints.suppressLogging(hints)) { + String logPrefix = Hints.getLogPrefix(hints); + logger.debug(logPrefix + "Writing '" + charSequence + "'"); } CharBuffer charBuffer = CharBuffer.wrap(charSequence); ByteBuffer byteBuffer = charset.encode(charBuffer); diff --git a/spring-core/src/main/java/org/springframework/core/codec/DataBufferEncoder.java b/spring-core/src/main/java/org/springframework/core/codec/DataBufferEncoder.java index 2ff10ce6cb..34cd61c89c 100644 --- a/spring-core/src/main/java/org/springframework/core/codec/DataBufferEncoder.java +++ b/spring-core/src/main/java/org/springframework/core/codec/DataBufferEncoder.java @@ -18,7 +18,6 @@ package org.springframework.core.codec; import java.util.Map; -import org.apache.commons.logging.Log; import org.reactivestreams.Publisher; import reactor.core.publisher.Flux; @@ -55,11 +54,11 @@ public class DataBufferEncoder extends AbstractEncoder { Flux flux = Flux.from(inputStream); - Log theLogger = Hints.getLoggerOrDefault(hints, logger); - if (theLogger.isDebugEnabled()) { - flux = flux.doOnNext(buffer -> - theLogger.debug(Hints.getLogPrefix(hints) + - "Writing " + buffer.readableByteCount() + " bytes")); + if (logger.isDebugEnabled() && !Hints.suppressLogging(hints)) { + flux = flux.doOnNext(buffer -> { + String logPrefix = Hints.getLogPrefix(hints); + logger.debug(logPrefix + "Writing " + buffer.readableByteCount() + " bytes"); + }); } return flux; diff --git a/spring-core/src/main/java/org/springframework/core/codec/Hints.java b/spring-core/src/main/java/org/springframework/core/codec/Hints.java index 87e3d4586c..56f5bd6c83 100644 --- a/spring-core/src/main/java/org/springframework/core/codec/Hints.java +++ b/spring-core/src/main/java/org/springframework/core/codec/Hints.java @@ -39,12 +39,12 @@ public abstract class Hints { public static final String LOG_PREFIX_HINT = Log.class.getName() + ".PREFIX"; /** - * Name of hint for a preferred {@link Log logger} to use. This can be used - * by a composite encoder (e.g. multipart requests) to control or suppress - * logging by individual part encoders. + * Name of boolean hint whether to avoid logging data either because it's + * potentially sensitive, or because it has been logged by a composite + * encoder, e.g. for multipart requests. * @since 5.1 */ - public static final String LOGGER_HINT = Log.class.getName(); + public static final String SUPPRESS_LOGGING_HINT = Log.class.getName() + ".SUPPRESS_LOGGING"; /** @@ -95,13 +95,12 @@ public abstract class Hints { } /** - * Obtain the hint {@link #LOGGER_HINT}, if present, or the given logger. - * @param hints the hints passed to the encode method - * @param defaultLogger the logger to return if a hint is not found - * @return the logger to use + * Whether to suppress logging based on the hint {@link #SUPPRESS_LOGGING_HINT}. + * @param hints the hints map + * @return whether logging of data is allowed */ - public static Log getLoggerOrDefault(@Nullable Map hints, Log defaultLogger) { - return hints != null ? (Log) hints.getOrDefault(LOGGER_HINT, defaultLogger) : defaultLogger; + public static boolean suppressLogging(@Nullable Map hints) { + return hints != null && (boolean) hints.getOrDefault(SUPPRESS_LOGGING_HINT, false); } /** diff --git a/spring-core/src/main/java/org/springframework/core/codec/ResourceEncoder.java b/spring-core/src/main/java/org/springframework/core/codec/ResourceEncoder.java index 41c2c90aca..61b1c80e14 100644 --- a/spring-core/src/main/java/org/springframework/core/codec/ResourceEncoder.java +++ b/spring-core/src/main/java/org/springframework/core/codec/ResourceEncoder.java @@ -18,7 +18,6 @@ package org.springframework.core.codec; import java.util.Map; -import org.apache.commons.logging.Log; import reactor.core.publisher.Flux; import org.springframework.core.ResolvableType; @@ -69,9 +68,9 @@ public class ResourceEncoder extends AbstractSingleValueEncoder { protected Flux encode(Resource resource, DataBufferFactory dataBufferFactory, ResolvableType type, @Nullable MimeType mimeType, @Nullable Map hints) { - Log theLogger = Hints.getLoggerOrDefault(hints, logger); - if (theLogger.isDebugEnabled()) { - theLogger.debug(Hints.getLogPrefix(hints) + "Writing [" + resource + "]"); + if (logger.isDebugEnabled() && !Hints.suppressLogging(hints)) { + String logPrefix = Hints.getLogPrefix(hints); + logger.debug(logPrefix + "Writing [" + resource + "]"); } return DataBufferUtils.read(resource, dataBufferFactory, this.bufferSize); diff --git a/spring-core/src/main/java/org/springframework/core/codec/ResourceRegionEncoder.java b/spring-core/src/main/java/org/springframework/core/codec/ResourceRegionEncoder.java index 96a9672ecf..e5add648d1 100644 --- a/spring-core/src/main/java/org/springframework/core/codec/ResourceRegionEncoder.java +++ b/spring-core/src/main/java/org/springframework/core/codec/ResourceRegionEncoder.java @@ -22,7 +22,6 @@ import java.nio.charset.StandardCharsets; import java.util.Map; import java.util.OptionalLong; -import org.apache.commons.logging.Log; import org.reactivestreams.Publisher; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -123,9 +122,8 @@ public class ResourceRegionEncoder extends AbstractEncoder { long position = region.getPosition(); long count = region.getCount(); - Log theLogger = Hints.getLoggerOrDefault(hints, logger); - if (theLogger.isDebugEnabled()) { - theLogger.debug(Hints.getLogPrefix(hints) + + if (logger.isDebugEnabled() && !Hints.suppressLogging(hints)) { + logger.debug(Hints.getLogPrefix(hints) + "Writing region " + position + "-" + (position + count) + " of [" + resource + "]"); } diff --git a/spring-web/src/main/java/org/springframework/http/codec/json/AbstractJackson2Encoder.java b/spring-web/src/main/java/org/springframework/http/codec/json/AbstractJackson2Encoder.java index b4f167a3a6..9db72ea1d8 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/json/AbstractJackson2Encoder.java +++ b/spring-web/src/main/java/org/springframework/http/codec/json/AbstractJackson2Encoder.java @@ -33,7 +33,6 @@ import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectWriter; import com.fasterxml.jackson.databind.exc.InvalidDefinitionException; -import org.apache.commons.logging.Log; import org.reactivestreams.Publisher; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -142,9 +141,8 @@ public abstract class AbstractJackson2Encoder extends Jackson2CodecSupport imple private DataBuffer encodeValue(Object value, @Nullable MimeType mimeType, DataBufferFactory bufferFactory, ResolvableType elementType, @Nullable Map hints, JsonEncoding encoding) { - Log theLogger = Hints.getLoggerOrDefault(hints, logger); - if (theLogger.isDebugEnabled()) { - theLogger.debug(Hints.getLogPrefix(hints) + "Encoding [" + value + "]"); + if (logger.isDebugEnabled() && !Hints.suppressLogging(hints)) { + logger.debug(Hints.getLogPrefix(hints) + "Encoding [" + value + "]"); } JavaType javaType = getJavaType(elementType.getType(), null); diff --git a/spring-web/src/main/java/org/springframework/http/codec/multipart/MultipartHttpMessageWriter.java b/spring-web/src/main/java/org/springframework/http/codec/multipart/MultipartHttpMessageWriter.java index 24dc8c0bd4..cc9d6db820 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/multipart/MultipartHttpMessageWriter.java +++ b/spring-web/src/main/java/org/springframework/http/codec/multipart/MultipartHttpMessageWriter.java @@ -29,7 +29,6 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Supplier; import java.util.stream.Collectors; -import org.apache.commons.logging.impl.NoOpLog; import org.reactivestreams.Publisher; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -87,7 +86,7 @@ public class MultipartHttpMessageWriter extends LoggingCodecSupport public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8; /** Suppress logging from individual part writers (full map logged at this level). */ - private static final Map DEFAULT_HINTS = Hints.from(Hints.LOGGER_HINT, new NoOpLog()); + private static final Map DEFAULT_HINTS = Hints.from(Hints.SUPPRESS_LOGGING_HINT, true); private final List> partWriters; diff --git a/spring-web/src/main/java/org/springframework/http/codec/xml/Jaxb2XmlEncoder.java b/spring-web/src/main/java/org/springframework/http/codec/xml/Jaxb2XmlEncoder.java index e5aec2d995..ac3140e179 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/xml/Jaxb2XmlEncoder.java +++ b/spring-web/src/main/java/org/springframework/http/codec/xml/Jaxb2XmlEncoder.java @@ -25,7 +25,6 @@ import javax.xml.bind.Marshaller; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; -import org.apache.commons.logging.Log; import reactor.core.publisher.Flux; import org.springframework.core.ResolvableType; @@ -75,9 +74,8 @@ public class Jaxb2XmlEncoder extends AbstractSingleValueEncoder { protected Flux encode(Object value, DataBufferFactory dataBufferFactory, ResolvableType type, @Nullable MimeType mimeType, @Nullable Map hints) { try { - Log theLogger = Hints.getLoggerOrDefault(hints, logger); - if (theLogger.isDebugEnabled()) { - theLogger.debug(Hints.getLogPrefix(hints) + "Encoding [" + value + "]"); + if (logger.isDebugEnabled() && !Hints.suppressLogging(hints)) { + logger.debug(Hints.getLogPrefix(hints) + "Encoding [" + value + "]"); } DataBuffer buffer = dataBufferFactory.allocateBuffer(1024); OutputStream outputStream = buffer.asOutputStream();