简体   繁体   中英

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM