Spring Boot - 如何在一个地方记录所有有异常的请求和响应? [英] Spring Boot - How to log all requests and responses with exceptions in single place?

查看:34
本文介绍了Spring Boot - 如何在一个地方记录所有有异常的请求和响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 spring boot 开发 rest api.我需要使用输入参数(使用方法,例如 GET、POST 等)、请求路径、查询字符串、此请求的相应类方法、此操作的响应以及成功和错误来记录所有请求.例如:

I'm working on rest api with spring boot. I need to log all requests with input params (with methods, eg. GET, POST, etc), request path, query string, corresponding class method of this request, also response of this action, both success and errors. For example:

请求成功:

http://example.com/api/users/1

日志应如下所示:

{
   HttpStatus: 200,
   path: "api/users/1",
   method: "GET",
   clientIp: "0.0.0.0",
   accessToken: "XHGu6as5dajshdgau6i6asdjhgjhg",
   method: "UsersController.getUser",
   arguments: {
     id: 1 
   },
   response: {
      user: {
        id: 1,
        username: "user123",
        email: "user123@example.com"   
      }
   },
   exceptions: []       
}

或请求出错:

http://example.com/api/users/9999

日志应该是这样的:

{
   HttpStatus: 404,
   errorCode: 101,                 
   path: "api/users/9999",
   method: "GET",
   clientIp: "0.0.0.0",
   accessToken: "XHGu6as5dajshdgau6i6asdjhgjhg",
   method: "UsersController.getUser",
   arguments: {
     id: 9999 
   },
   returns: {            
   },
   exceptions: [
     {
       exception: "UserNotFoundException",
       message: "User with id 9999 not found",
       exceptionId: "adhaskldjaso98d7324kjh989",
       stacktrace: ...................    
   ]       
}

我希望请求/响应是单个实体,在成功和错误情况下都包含与该实体相关的自定义信息.

I want Request/Response to be a single entity, with custom information related to this entity, both in successful and error cases.

春季实现这一目标的最佳实践是什么,可能是使用过滤器?如果是,你能提供具体的例子吗?

What is best practice in spring to achieve this, may be with filters? if yes, can you provide concrete example?

我玩过 @ControllerAdvice@ExceptionHandler,但正如我提到的,我需要在一个地方(和单个日志)处理所有成功和错误请求.

I've played with @ControllerAdvice and @ExceptionHandler, but as I mentioned, I need to handle all success and error requests in single place (and single log).

推荐答案

不要写任何拦截器、过滤器、组件、方面等,这是一个很常见的问题,已经解决了很多次了.

Don't write any Interceptors, Filters, Components, Aspects, etc., this is a very common problem and has been solved many times over.

Spring Boot 有一个模块叫做 Actuator,提供开箱即用的 HTTP 请求登录.有一个映射到 /trace (SB1.x) 或 /actuator/httptrace (SB2.0+) 的端点,它将显示最近的 100 个 HTTP 请求.您可以自定义它以记录每个请求,或写入数据库.

Spring Boot has a modules called Actuator, which provides HTTP request logging out of the box. There's an endpoint mapped to /trace (SB1.x) or /actuator/httptrace (SB2.0+) which will show you last 100 HTTP requests. You can customize it to log each request, or write to a DB.

要获得您想要的端点,您将需要 spring-boot-starter-actuator 依赖项,并且还将您要查找的端点列入白名单",并可能为其设置或禁用安全性.

To get the endpoints you want, you'll need the spring-boot-starter-actuator dependency, and also to "whitelist" the endpoints you're looking for, and possibly setup or disable security for it.

另外,这个应用程序将在哪里运行?你会使用 PaaS 吗?托管服务提供商(例如 Heroku)提供请求日志记录作为其服务的一部分,您无需进行任何编码.

Also, where will this application run? Will you be using a PaaS? Hosting providers, Heroku for example, provide request logging as part of their service and you don't need to do any coding whatsoever then.

这篇关于Spring Boot - 如何在一个地方记录所有有异常的请求和响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆