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

View File

@ -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<String, Object> 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);

View File

@ -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<String, Object> DEFAULT_HINTS = Hints.from(Hints.LOGGER_HINT, new NoOpLog());
private static final Map<String, Object> DEFAULT_HINTS = Hints.from(Hints.SUPPRESS_LOGGING_HINT, true);
private final List<HttpMessageWriter<?>> partWriters;

View File

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