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

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

问题描述

我正在使用弹簧靴进行休息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.

例如:

成功请求:

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

日志应该看起来像这样:

Log should be looked something like this:

{
   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)).

推荐答案

不要写任何拦截器,过滤器,组件,方面等。 ,这是一个非常普遍的问题,已经解决了很多次。 Spring Boot有一个名为执行器的模块提供开箱即用的HTTP请求记录。有一个端点映射到 / trace (SB1.x)或 / actuator / httptrace (SB2.0 +)将显示最近100个HTTP请求。您可以自定义它以记录每个请求,或写入数据库。要获得所需的端点,您需要执行器springboot依赖项,并且还要查找您正在寻找的端点,并可能设置或禁用它的安全性。

Don't write any Interceptors, Filters, Components, Aspects, etc., this is a very common problem and has been solved many times over. 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. To get the endpoints you want, you'll need the actuator springboot 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天全站免登陆