【问题标题】:In spring boot 2 whats the most efficient way to log body?在 Spring Boot 2 中记录正文的最有效方法是什么?
【发布时间】:2018-12-24 13:31:53
【问题描述】:

我曾经使用这个在 JAX-RS 的过滤器中记录我的请求

@Provider
public class RequestLoggingFilter implements ContainerRequestFilter {
    @Context
    private HttpServletRequest servletRequest;

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {

        BufferedInputStream stream = new 
        BufferedInputStream(requestContext.getEntityStream());
        String payload = IOUtils.toString(stream, "UTF-8");
        requestContext.setEntityStream(IOUtils.toInputStream(payload, "UTF-8"));
    }

}

我正在尝试在 spring boot 2 中做类似的事情,我尝试过使用过滤器、拦截器和执行器。我一读就失去了身体。在 Spring Boot 2 中,执行器的跟踪不支持记录主体,那么有没有其他方法可以记录请求和响应主体而不会丢失它?

【问题讨论】:

标签: java spring-boot logging jax-rs spring-boot-actuator


【解决方案1】:

您可能会在那里找到所需的一切:http://www.baeldung.com/spring-http-logging

Spring 提供了一个内置的解决方案来记录有效负载。我们可以用 通过插入 Spring 应用程序使用现成的过滤器 配置。

AbstractRequestLoggingFilter 是一个过滤器,它提供基本的 记录的功能。子类应该覆盖 beforeRequest() 和 afterRequest() 方法来执行实际的日志记录 请求。

Spring 框架提供了三个具体的实现类 可用于记录传入请求。这三个类是:

CommonsRequestLoggingFilter Log4jNestedDiagnosticContextFilter (已弃用)ServletContextRequestLoggingFilter 现在,让我们继续 CommonsRequestLoggingFilter 并将其配置为捕获传入的 请求记录。

【讨论】:

  • 我看到 CommonsRequestLoggingFilter 确实记录了请求,但是在那个链接中,我确实尝试覆盖了 CustomeRequestLoggingFilter 中的 beforeRequest,这几乎不起作用。即使该函数被覆盖,也会调用 CommonsRequestLoggingFilter
猜你喜欢
  • 2018-11-10
  • 1970-01-01
  • 2017-04-27
  • 2017-06-12
  • 1970-01-01
  • 2015-04-04
  • 2012-03-26
  • 1970-01-01
  • 2018-05-31
相关资源
最近更新 更多