Polish hint for suppressing logging at Encoder/Decoder

This commit is contained in:
Rossen Stoyanchev
2018-07-06 20:32:08 -04:00
parent 2874dd75ca
commit bca9f51092
10 changed files with 33 additions and 48 deletions

View File

@@ -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<byte[]> {
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;
});

View File

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

View File

@@ -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<CharSequence> {
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);

View File

@@ -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<DataBuffer> {
Flux<DataBuffer> 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;

View File

@@ -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<String, Object> hints, Log defaultLogger) {
return hints != null ? (Log) hints.getOrDefault(LOGGER_HINT, defaultLogger) : defaultLogger;
public static boolean suppressLogging(@Nullable Map<String, Object> hints) {
return hints != null && (boolean) hints.getOrDefault(SUPPRESS_LOGGING_HINT, false);
}
/**

View File

@@ -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<Resource> {
protected Flux<DataBuffer> encode(Resource resource, DataBufferFactory dataBufferFactory,
ResolvableType type, @Nullable MimeType mimeType, @Nullable Map<String, Object> 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);

View File

@@ -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<ResourceRegion> {
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 + "]");
}