简体   繁体   English

在 Spring Boot 中抑制异常的警告日志

[英]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:但是当我从@RestController 中抛出它时,它会记录以下内容:

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 .您可以将类org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver日志级别设置为ERROR That way you will only loose WARN , DEBUG and TRACE logs for this class only.这样你只会丢失这个类的WARNDEBUGTRACE日志。

If there are some WARN-level exceptions you want to see from this class I see two options:如果您想从此类中看到一些 WARN 级别的异常,我会看到两个选项:

  • copy org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver to your codebase, change the logging.org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver复制到您的代码库,更改日志记录。 Make sure that your copy is on the top of the classpath and Spring is below.确保您的副本位于类路径的顶部,而 Spring 位于下方。 This is kind of hack.这是一种黑客攻击。 ResponseStatusExceptionResolver is implementation detail of Spring. ResponseStatusExceptionResolver 是 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.编写自己的 org.springframework.web.servlet.HandlerExceptionResolver 实现,注册它,确保 ResponseStatusExceptionResolver 没有注册。

In Spring you can add this line into your application.properties to specify logging level for certain class:在 Spring 中,您可以将此行添加到application.properties以指定特定类的日志记录级别:

logging.level.site.user.AccessException=ERROR

or

logging.level.site.user.AccessException=OFF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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