Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I have a Spring Boot application with a 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.

like image 548
Gary Avatar asked Jun 20 '17 01:06

Gary


People also ask

How do I create a global exception in spring boot?

Exception Handler The @ExceptionHandler is an annotation used to handle the specific exceptions and sending the custom responses to the client. Define a class that extends the RuntimeException class. You can define the @ExceptionHandler method to handle the exceptions as shown.

How does spring controller handle exceptions?

Spring MVC provides exception handling for your web application to make sure you are sending your own exception page instead of the server-generated exception to the user. The @ExceptionHandler annotation is used to detect certain runtime exceptions and send responses according to the exception.

How do you throw an exception using ResponseEntity?

You have to provide implementation to use your error handler, map the response to response entity and throw the exception. Create new error exception class with ResponseEntity field. Custom error handler which maps the error response back to ResponseEntity.

How do you handle exceptions in spring boot REST API?

Altogether, the most common way is to use @ExceptionHandler on methods of @ControllerAdvice classes so that the exception handling will be applied globally or to a subset of controllers. ControllerAdvice is an annotation introduced in Spring 3.2, and as the name suggests, is “Advice” for multiple controllers.


1 Answers

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.

like image 148
Sotirios Delimanolis Avatar answered Sep 18 '22 15:09

Sotirios Delimanolis