Add LogFormatUtils
1. Helper method to eliminate duplication in formatting (de-)serialized values for logging introduced with prior commit #e62298. 2. Helper method for TRACE vs DEBUG logging with different details. Issue: SPR-17254
This commit is contained in:
@@ -48,6 +48,7 @@ import org.springframework.context.i18n.LocaleContext;
|
||||
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.support.PropertiesLoaderUtils;
|
||||
import org.springframework.core.log.LogFormatUtils;
|
||||
import org.springframework.http.server.ServletServerHttpRequest;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.ui.context.ThemeSource;
|
||||
@@ -951,7 +952,7 @@ public class DispatcherServlet extends FrameworkServlet {
|
||||
}
|
||||
|
||||
private void logRequest(HttpServletRequest request) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
LogFormatUtils.traceDebug(logger, traceOn -> {
|
||||
String params;
|
||||
if (isEnableLoggingRequestDetails()) {
|
||||
params = request.getParameterMap().entrySet().stream()
|
||||
@@ -968,19 +969,19 @@ public class DispatcherServlet extends FrameworkServlet {
|
||||
String message = (dispatchType + request.getMethod() + " \"" + getRequestUri(request) +
|
||||
query + "\", parameters={" + params + "}");
|
||||
|
||||
if (logger.isTraceEnabled()) {
|
||||
if (traceOn) {
|
||||
List<String> values = Collections.list(request.getHeaderNames());
|
||||
String headers = values.size() > 0 ? "masked" : "";
|
||||
if (isEnableLoggingRequestDetails()) {
|
||||
headers = values.stream().map(name -> name + ":" + Collections.list(request.getHeaders(name)))
|
||||
.collect(Collectors.joining(", "));
|
||||
}
|
||||
logger.trace(message + ", headers={" + headers + "} in DispatcherServlet '" + getServletName() + "'");
|
||||
return message + ", headers={" + headers + "} in DispatcherServlet '" + getServletName() + "'";
|
||||
}
|
||||
else {
|
||||
logger.debug(message);
|
||||
return message;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1082,7 +1082,9 @@ public abstract class FrameworkServlet extends HttpServletBean implements Applic
|
||||
if (failureCause != null) {
|
||||
if (!initialDispatch) {
|
||||
// FORWARD/ERROR/ASYNC: minimal message (there should be enough context already)
|
||||
logger.debug("Unresolved failure from \"" + dispatchType + "\" dispatch: " + failureCause);
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Unresolved failure from \"" + dispatchType + "\" dispatch: " + failureCause);
|
||||
}
|
||||
}
|
||||
else if (logger.isTraceEnabled()) {
|
||||
logger.trace("Failed to complete request", failureCause);
|
||||
|
||||
@@ -37,6 +37,7 @@ import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.core.log.LogFormatUtils;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpInputMessage;
|
||||
import org.springframework.http.HttpMethod;
|
||||
@@ -223,17 +224,12 @@ public abstract class AbstractMessageConverterMethodArgumentResolver implements
|
||||
throw new HttpMediaTypeNotSupportedException(contentType, this.allSupportedMediaTypes);
|
||||
}
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
boolean traceOn = logger.isTraceEnabled();
|
||||
String s = "Read \"" + contentType + "\" to [" +
|
||||
RequestMappingHandlerAdapter.formatValue(body, traceOn) + "]";
|
||||
if (traceOn) {
|
||||
logger.trace(s);
|
||||
}
|
||||
else {
|
||||
logger.debug(s);
|
||||
}
|
||||
}
|
||||
MediaType selectedContentType = contentType;
|
||||
Object theBody = body;
|
||||
LogFormatUtils.traceDebug(logger, traceOn -> {
|
||||
String formatted = LogFormatUtils.formatValue(theBody, !traceOn);
|
||||
return "Read \"" + selectedContentType + "\" to [" + formatted + "]";
|
||||
});
|
||||
|
||||
return body;
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.io.InputStreamResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.support.ResourceRegion;
|
||||
import org.springframework.core.log.LogFormatUtils;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpOutputMessage;
|
||||
@@ -280,16 +281,9 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe
|
||||
(Class<? extends HttpMessageConverter<?>>) converter.getClass(),
|
||||
inputMessage, outputMessage);
|
||||
if (body != null) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
boolean traceOn = logger.isTraceEnabled();
|
||||
String s = "Writing [" + RequestMappingHandlerAdapter.formatValue(body, traceOn) + "]";
|
||||
if (traceOn) {
|
||||
logger.trace(s);
|
||||
}
|
||||
else {
|
||||
logger.debug(s);
|
||||
}
|
||||
}
|
||||
Object theBody = body;
|
||||
LogFormatUtils.traceDebug(logger, traceOn ->
|
||||
"Writing [" + LogFormatUtils.formatValue(theBody, traceOn) + "]");
|
||||
addContentDispositionHeader(inputMessage, outputMessage);
|
||||
if (genericConverter != null) {
|
||||
genericConverter.write(body, targetType, selectedMediaType, outputMessage);
|
||||
|
||||
@@ -38,6 +38,7 @@ import org.springframework.core.ParameterNameDiscoverer;
|
||||
import org.springframework.core.ReactiveAdapterRegistry;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
||||
import org.springframework.core.log.LogFormatUtils;
|
||||
import org.springframework.core.task.AsyncTaskExecutor;
|
||||
import org.springframework.core.task.SimpleAsyncTaskExecutor;
|
||||
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
|
||||
@@ -884,15 +885,10 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter
|
||||
Object result = asyncManager.getConcurrentResult();
|
||||
mavContainer = (ModelAndViewContainer) asyncManager.getConcurrentResultContext()[0];
|
||||
asyncManager.clearConcurrentResult();
|
||||
if (logger.isDebugEnabled()) {
|
||||
String s = "Resume with async result [" + formatValue(result, logger.isTraceEnabled()) + "]";
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace(s);
|
||||
}
|
||||
else {
|
||||
logger.debug(s);
|
||||
}
|
||||
}
|
||||
LogFormatUtils.traceDebug(logger, traceOn -> {
|
||||
String formatted = LogFormatUtils.formatValue(result, !traceOn);
|
||||
return "Resume with async result [" + formatted + "]";
|
||||
});
|
||||
invocableMethod = invocableMethod.wrapConcurrentResult(result);
|
||||
}
|
||||
|
||||
@@ -1024,12 +1020,4 @@ 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)...";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user