How do I create a Spring @ExceptionHandler method that does not log a WARN message to the log

Gary :

I have this Spring @ExceptionHandler that mostly does what I want. It sets the HTTP status code to 409 and includes extra error information in the JSON response.

@ExceptionHandler(PolicyExecutionException.class)
public ResponseEntity handleException(PolicyExecutionException se){
    return ResponseEntity.status(HttpStatus.CONFLICT).body(se.getScriptErrorMap());
}

What I've noticed is that even though it delivers the correct response to the client, the Spring is logging a message at the WARN level. My message is quite long and only relevant to the client. I'd rather not log it at all on the server side. Can this be turned off?

2017-06-19 18:48:10,659 [p-nio-8060-exec-2] WARN o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolved exception caused by Handler execution:

One alternative I have used is to catch the PolicyExecutionException on each of the Controller methods where it could be thrown. This leads to extra boilerplate I'd rather avoid if possible.

Sotirios Delimanolis :

AbstractHandlerExceptionResolver, a supertype of ExceptionHandlerExceptionResolver, maintains a warnLogger for logging that message when an exception is resolved, controlled by setWarnLogCategory.

Default is no warn logging. Specify this setting to activate warn logging into a specific category. Alternatively, override the logException(java.lang.Exception, javax.servlet.http.HttpServletRequest) method for custom logging.

When you create your ExceptionHandlerExceptionResolver (or when it's created for you through default configuration), it's set to null and not used.

With Spring Boot, you can control whether it's enabled with the

spring.mvc.logResolvedException

application property. This controls the corresponding value in WebMvcProperties. (It's false by default so you must have set it to true in your configuration, or you've called setWarnLogCategory explicitly.)


Alternatively, turn off the logger.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=444818&siteId=1