【发布时间】:2019-03-13 20:41:34
【问题描述】:
我有一个为 REST API 运行 Netty 的 Spring Boot 应用程序。
我有一个 WebExceptionHandler 来处理可能发生的异常,它正在工作。它构建适当的响应并发送它们。
问题是,它仍然将异常记录为错误。我想将其更改为将其记录为信息(因为我们有根据信息或错误进行不同操作的跟踪和警报)。它甚至会将 404 之类的内容记录为错误。
似乎exceptionCaught in a ChannelInboundHandler 会有所帮助,但exceptionCaught 已被弃用。我找不到任何不使用这种方法的东西,也找不到任何指代它的东西(如果有的话)。
我也尝试使用@ControllerAdvice 或@RestControllerAdvice 和@ExceptionHandler,但从未调用过。
拦截和处理异常日志的正确方法是什么?
我们当前设置的最小示例实现如下所示:
@RestController
class MyController {
@RequestMapping(method = [GET], value = ["endpoint"], produces = [APPLICATION_JSON_VALUE])
fun myEndpoint(): Mono<MyResponse> = createResponse()
}
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
class MyWebExceptionHandler : WebExceptionHandler {
// This does get called and sends the appropriate response back, but an error is logged somewhere outside of our code.
override fun handle(exchange: ServerWebExchange, ex: Throwable): Mono<Void> =
createErrorResponse();
}
// Tried using both, or just one at a time, no difference.
// It does get created.
@ControllerAdvice
@RestControllerAdvice
class MyExceptionHandler {
// Never called
@ExceptionHandler(Exception::class)
fun handle(ex: Exception) {
log.error(ex.message)
}
}
【问题讨论】:
标签: java spring spring-boot kotlin netty