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

@@ -224,8 +224,15 @@ public abstract class AbstractMessageConverterMethodArgumentResolver implements
}
if (logger.isDebugEnabled()) {
logger.debug("Read \"" + contentType + "\" to " +
"[" + (body instanceof String ? "\"" + body + "\"" : body) + "]");
boolean traceOn = logger.isTraceEnabled();
String s = "Read \"" + contentType + "\" to [" +
RequestMappingHandlerAdapter.formatValue(body, traceOn) + "]";
if (traceOn) {
logger.trace(s);
}
else {
logger.debug(s);
}
}
return body;

View File

@@ -281,7 +281,14 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe
inputMessage, outputMessage);
if (body != null) {
if (logger.isDebugEnabled()) {
logger.debug("Writing [" + formatValue(body) + "]");
boolean traceOn = logger.isTraceEnabled();
String s = "Writing [" + RequestMappingHandlerAdapter.formatValue(body, traceOn) + "]";
if (traceOn) {
logger.trace(s);
}
else {
logger.debug(s);
}
}
addContentDispositionHeader(inputMessage, outputMessage);
if (genericConverter != null) {
@@ -398,10 +405,6 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe
return (MediaType.SPECIFICITY_COMPARATOR.compare(acceptType, produceTypeToUse) <= 0 ? acceptType : produceTypeToUse);
}
static String formatValue(Object body) {
return (body instanceof CharSequence ? "\"" + body + "\"" : body.toString());
}
/**
* Check if the path has a file extension and whether the extension is
* either {@link #WHITELISTED_EXTENSIONS whitelisted} or explicitly

View File

@@ -885,8 +885,13 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter
mavContainer = (ModelAndViewContainer) asyncManager.getConcurrentResultContext()[0];
asyncManager.clearConcurrentResult();
if (logger.isDebugEnabled()) {
String formatted = AbstractMessageConverterMethodProcessor.formatValue(result);
logger.debug("Resume with async result [" + formatted + "]");
String s = "Resume with async result [" + formatValue(result, logger.isTraceEnabled()) + "]";
if (logger.isTraceEnabled()) {
logger.trace(s);
}
else {
logger.debug(s);
}
}
invocableMethod = invocableMethod.wrapConcurrentResult(result);
}
@@ -1019,4 +1024,12 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter
return mav;
}
static String formatValue(@Nullable Object body, boolean logFullBody) {
if (body == null) {
return "";
}
String s = body instanceof CharSequence ? "\"" + body + "\"" : body.toString();
return logFullBody || s.length() < 100 ? s : s.substring(0, 100) + " (truncated)...";
}
}