James Watkins
James Watkins

Reputation: 4954

Suppress warn logs for exception in Spring Boot

I have a custom exception like this

@ResponseStatus(HttpStatus.UNAUTHORIZED)
public class AccessException extends RuntimeException {
}

But when I throw it from a @RestController, it's logging this:

2018-04-17 11:44:58.239  WARN 17928 --- [nio-8080-exec-3] .w.s.m.a.ResponseStatusExceptionResolver : Resolved exception caused by Handler execution: site.user.AccessException

I tried checking the source code but could not find where this is getting logged. I don't want to turn off logging because it may log other things that I am interested in. I just don't think that a custom exception like this should be logged every time.

Upvotes: 5

Views: 2726

Answers (2)

Zon
Zon

Reputation: 19880

In Spring you can add this line into your application.properties to specify logging level for certain class:

logging.level.site.user.AccessException=ERROR

or

logging.level.site.user.AccessException=OFF

Upvotes: 0

Bartosz Bilicki
Bartosz Bilicki

Reputation: 13235

You may set log level for class org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver to ERROR. That way you will only loose WARN, DEBUG and TRACE logs for this class only.

If there are some WARN-level exceptions you want to see from this class I see two options:

  • copy org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver to your codebase, change the logging. Make sure that your copy is on the top of the classpath and Spring is below. This is kind of hack. ResponseStatusExceptionResolver is implementation detail of Spring. It may be changed/moved/deleted in future release.
  • write your own implementation of org.springframework.web.servlet.HandlerExceptionResolver, register it, make sure that ResponseStatusExceptionResolver is not registered.

Upvotes: 1

Related Questions