Apply value formatting to resolved exceptions

This commit is contained in:
Rossen Stoyanchev 2021-10-11 11:14:02 +01:00
parent 47b8e8d528
commit e8f6cd10a5
3 changed files with 9 additions and 6 deletions

View File

@ -43,7 +43,7 @@ public abstract class LogFormatUtils {
* @return the formatted value * @return the formatted value
*/ */
public static String formatValue(@Nullable Object value, boolean limitLength) { public static String formatValue(@Nullable Object value, boolean limitLength) {
return formatValue(value, 100, limitLength); return formatValue(value, (limitLength ? 100 : -1), limitLength);
} }
/** /**

View File

@ -20,6 +20,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import org.springframework.core.log.LogFormatUtils;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.http.server.reactive.ServerHttpResponse;
@ -81,9 +82,10 @@ public class ResponseStatusExceptionHandler implements WebExceptionHandler {
private String formatError(Throwable ex, ServerHttpRequest request) { private String formatError(Throwable ex, ServerHttpRequest request) {
String reason = ex.getClass().getSimpleName() + ": " + ex.getMessage(); String className = ex.getClass().getSimpleName();
String message = LogFormatUtils.formatValue(ex.getMessage(), -1, true);
String path = request.getURI().getRawPath(); String path = request.getURI().getRawPath();
return "Resolved [" + reason + "] for HTTP " + request.getMethod() + " " + path; return "Resolved [" + className + ": " + message + "] for HTTP " + request.getMethod() + " " + path;
} }
private boolean updateResponse(ServerHttpResponse response, Throwable ex) { private boolean updateResponse(ServerHttpResponse response, Throwable ex) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.log.LogFormatUtils;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.HandlerExceptionResolver;
@ -142,7 +143,7 @@ public abstract class AbstractHandlerExceptionResolver implements HandlerExcepti
if (result != null) { if (result != null) {
// Print debug message when warn logger is not enabled. // Print debug message when warn logger is not enabled.
if (logger.isDebugEnabled() && (this.warnLogger == null || !this.warnLogger.isWarnEnabled())) { if (logger.isDebugEnabled() && (this.warnLogger == null || !this.warnLogger.isWarnEnabled())) {
logger.debug("Resolved [" + ex + "]" + (result.isEmpty() ? "" : " to " + result)); logger.debug(buildLogMessage(ex, request) + (result.isEmpty() ? "" : " to " + result));
} }
// Explicitly configured warn logger in logException method. // Explicitly configured warn logger in logException method.
logException(ex, request); logException(ex, request);
@ -215,7 +216,7 @@ public abstract class AbstractHandlerExceptionResolver implements HandlerExcepti
* @return the log message to use * @return the log message to use
*/ */
protected String buildLogMessage(Exception ex, HttpServletRequest request) { protected String buildLogMessage(Exception ex, HttpServletRequest request) {
return "Resolved [" + ex + "]"; return "Resolved [" + LogFormatUtils.formatValue(ex, -1, true) + "]";
} }
/** /**