简体   繁体   English

Java Spring Boot - 使用有效负载记录响应

[英]Java Spring Boot - logging response with payloads

I'm currently integrating request / response logging into a REST service using Spring Boot. 我目前正在使用Spring Boot将请求/响应日志记录集成到REST服务中。 For requests, I chose the CommonsRequestLoggingFilter as provided by Spring: 对于请求,我选择了Spring提供的CommonsRequestLoggingFilter:

@Bean
public CommonsRequestLoggingFilter requestLoggingFilter() {
    CommonsRequestLoggingFilter loggingFilter = new CommonsRequestLoggingFilter();
    loggingFilter.setIncludeClientInfo(false);
    loggingFilter.setIncludeQueryString(true);
    loggingFilter.setIncludePayload(true);
    loggingFilter.setMaxPayloadLength(1024);

    return loggingFilter;
}

And in the configuration file: 并在配置文件中:

logging.level.org.springframework.web.filter.CommonsRequestLoggingFilter=DEBUG

For reponses, however, there seems to be no corresponding class? 然而,对于回应,似乎没有相应的类? Is there a similar way to log the server response in Spring? 是否有类似的方法在Spring中记录服务器响应?

EDIT: Specically, what I see with the above setup is nothing in BeforeRequest: 编辑:特别是,我在上面的设置中看到的内容在BeforeRequest中没有任何内容:

2017-06-28 09:32:32.258 DEBUG 22872 --- [http-nio-8081-exec-2] o.s.w.f.CommonsRequestLoggingFilter      : Before request [uri=/someApp/someObject]

The request payload as AfterRequest: 请求有效负载为AfterRequest:

2017-06-28 09:32:32.272 DEBUG 22872 --- [http-nio-8081-exec-2] o.s.w.f.CommonsRequestLoggingFilter      : 
After request [uri=/someApp/someResource;payload={
  "someObject":   {
                    "lastName": "Doe",
                    "reference": "123456789"
              }
    }
]

And the actual response is nowhere in the log. 并且日志中没有实际响应。

Nevermind, 没关系,

I ended up implementing my own filter that can log responses as well. 我最终实现了自己的过滤器,也可以记录响应。 Based on GenericFilterBean, wrapping HttpResponse and HttpRequest so the streams don't get closed. 基于GenericFilterBean,包装HttpResponse和HttpRequest,因此流不会被关闭。

It's still strange to me that Spring provides a class for logging requests, but not for responses... 我仍然很奇怪,Spring提供了一个用于记录请求的类,但不提供响应...

If you need to monitor and manage your app consider to use actuator . 如果您需要监控和管理您的应用程序,请考虑使用执行器 It has ability to log requests\\responses out of the box. 它能够开箱即用地记录请求\\响应。

您可以尝试在application.properties中设置以下行来记录请求/响应

logging.level.org.springframework.ws.server.MessageTracing.sent=TRACE

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

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