Skip to content

Commit bf7fa39

Browse files
committed
Consistent logging of resolved exceptions
Issue: SPR-17178
1 parent 37db3ba commit bf7fa39

File tree

2 files changed

+10
-18
lines changed

2 files changed

+10
-18
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerExceptionResolver.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,16 @@ public ModelAndView resolveException(
132132
HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) {
133133

134134
if (shouldApplyTo(request, handler)) {
135-
if (this.logger.isDebugEnabled()) {
136-
this.logger.debug("Resolving exception from handler [" + handler + "]: " + ex);
137-
}
138135
prepareResponse(ex, response);
139136
ModelAndView result = doResolveException(request, response, handler, ex);
140137
if (result != null) {
138+
139+
// Print warn message, when warn logger is not enabled..
140+
if (logger.isWarnEnabled() && (this.warnLogger == null || !this.warnLogger.isWarnEnabled())) {
141+
logger.warn("Resolved [" + ex + "]" + (result.isEmpty() ? "" : " to " + result));
142+
}
143+
144+
// warnLogger with full stack trace (requires explicit config)..
141145
logException(ex, request);
142146
}
143147
return result;

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,6 @@ else if (ex instanceof AsyncRequestTimeoutException) {
252252
protected ModelAndView handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex,
253253
HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {
254254

255-
pageNotFoundLogger.warn(ex.getMessage());
256255
String[] supportedMethods = ex.getSupportedMethods();
257256
if (supportedMethods != null) {
258257
response.setHeader("Allow", StringUtils.arrayToDelimitedString(supportedMethods, ", "));
@@ -376,9 +375,6 @@ protected ModelAndView handleServletRequestBindingException(ServletRequestBindin
376375
protected ModelAndView handleConversionNotSupported(ConversionNotSupportedException ex,
377376
HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {
378377

379-
if (logger.isWarnEnabled()) {
380-
logger.warn("Failed to convert request element: " + ex);
381-
}
382378
sendServerError(ex, request, response);
383379
return new ModelAndView();
384380
}
@@ -397,9 +393,6 @@ protected ModelAndView handleConversionNotSupported(ConversionNotSupportedExcept
397393
protected ModelAndView handleTypeMismatch(TypeMismatchException ex,
398394
HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {
399395

400-
if (logger.isWarnEnabled()) {
401-
logger.warn("Failed to bind request element: " + ex);
402-
}
403396
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
404397
return new ModelAndView();
405398
}
@@ -420,9 +413,6 @@ protected ModelAndView handleTypeMismatch(TypeMismatchException ex,
420413
protected ModelAndView handleHttpMessageNotReadable(HttpMessageNotReadableException ex,
421414
HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {
422415

423-
if (logger.isWarnEnabled()) {
424-
logger.warn("Failed to read HTTP message: " + ex);
425-
}
426416
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
427417
return new ModelAndView();
428418
}
@@ -444,9 +434,6 @@ protected ModelAndView handleHttpMessageNotReadable(HttpMessageNotReadableExcept
444434
protected ModelAndView handleHttpMessageNotWritable(HttpMessageNotWritableException ex,
445435
HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {
446436

447-
if (logger.isWarnEnabled()) {
448-
logger.warn("Failed to write HTTP message: " + ex);
449-
}
450437
sendServerError(ex, request, response);
451438
return new ModelAndView();
452439
}
@@ -520,6 +507,7 @@ protected ModelAndView handleBindException(BindException ex, HttpServletRequest
520507
protected ModelAndView handleNoHandlerFoundException(NoHandlerFoundException ex,
521508
HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {
522509

510+
pageNotFoundLogger.warn(ex.getMessage());
523511
response.sendError(HttpServletResponse.SC_NOT_FOUND);
524512
return new ModelAndView();
525513
}
@@ -542,8 +530,8 @@ protected ModelAndView handleAsyncRequestTimeoutException(AsyncRequestTimeoutExc
542530
if (!response.isCommitted()) {
543531
response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
544532
}
545-
else if (logger.isDebugEnabled()) {
546-
logger.debug("Async timeout for " + request.getMethod() + " [" + request.getRequestURI() + "]");
533+
else {
534+
logger.warn("Async request timed out");
547535
}
548536
return new ModelAndView();
549537
}

0 commit comments

Comments
 (0)