Truncate logged encoded and decoded values if necessary

At DEBUG show up to 100 chars, at TRACE show full formatted value.

Note that the formatValue helper method is duplicated a number of times
in this commit. A utility method will likely be added in spring-core
through an extra commit.

Issue: SPR-17254
This commit is contained in:
Rossen Stoyanchev
2018-09-14 12:20:03 -04:00
parent 66c66baa8f
commit e62298eaad
16 changed files with 187 additions and 32 deletions

View File

@@ -70,7 +70,13 @@ public final class CharSequenceEncoder extends AbstractEncoder<CharSequence> {
return Flux.from(inputStream).map(charSequence -> {
if (logger.isDebugEnabled() && !Hints.isLoggingSuppressed(hints)) {
String logPrefix = Hints.getLogPrefix(hints);
logger.debug(logPrefix + "Writing '" + charSequence + "'");
String s = logPrefix + "Writing " + formatValue(charSequence, logger.isTraceEnabled());
if (logger.isTraceEnabled()) {
logger.trace(s);
}
else {
logger.debug(s);
}
}
CharBuffer charBuffer = CharBuffer.wrap(charSequence);
ByteBuffer byteBuffer = charset.encode(charBuffer);
@@ -89,6 +95,15 @@ public final class CharSequenceEncoder extends AbstractEncoder<CharSequence> {
return charset;
}
private String formatValue(@Nullable Object value, boolean logFullValue) {
if (value == null) {
return "";
}
String s = value instanceof CharSequence ? "\"" + value + "\"" : value.toString();
return logFullValue || s.length() < 100 ? s : s.substring(0, 100) + " (truncated)...";
}
/**
* Create a {@code CharSequenceEncoder} that supports only "text/plain".
*/

View File

@@ -207,7 +207,13 @@ public final class StringDecoder extends AbstractDataBufferDecoder<String> {
DataBufferUtils.release(dataBuffer);
String value = charBuffer.toString();
if (logger.isDebugEnabled()) {
logger.debug(Hints.getLogPrefix(hints) + "Decoded '" + value + "'");
String s = Hints.getLogPrefix(hints) + "Decoded " + formatValue(value, logger.isTraceEnabled());
if (logger.isTraceEnabled()) {
logger.trace(s);
}
else {
logger.debug(s);
}
}
return value;
}
@@ -221,6 +227,14 @@ public final class StringDecoder extends AbstractDataBufferDecoder<String> {
}
}
private String formatValue(@Nullable Object value, boolean logFullValue) {
if (value == null) {
return "";
}
String s = value instanceof CharSequence ? "\"" + value + "\"" : value.toString();
return logFullValue || s.length() < 100 ? s : s.substring(0, 100) + " (truncated)...";
}
/**
* Create a {@code StringDecoder} for {@code "text/plain"}.