如何在 spring 休息中记录所有请求响应? [英] How to log all the request-responses in spring rest?

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

问题描述

应用程序应在不影响客户端的情况下异步(在单独的线程中)记录以下信息.

The application should log the following information without impacting a client, asynchronously(in a separate thread).

  • 请求 HTTP 方法和 URI
  • 请求标头(默认除外)
  • 客户的 IP 地址
  • 请求处理时间(以毫秒为单位)
  • 请求正文
  • 响应正文

如果我们在过滤器中使用inputstream,那么spring就不能再使用它来进行json到对象的映射.在输入流到对象映射期间的某个地方,我们可以插入我们的记录器吗?

If we consume inputstream in the filter, then it cant be consumed again by spring for json to object mapping. Somewhere during the input stream to object mapping, can we plug our logger?

更新:

我们可以在 MessageConverter 中重写日志代码,但它似乎不是一个好方法想法.

We can write over logging code in a MessageConverter, but it doesnt seems to be a good idea.

public class MyMappingJackson2MessageConverter extends AbstractHttpMessageConverter<Object> {
    ...
    protected Object readInternal(Class<? extends Object> clazz, HttpInputMessage inputMessage)
            throws IOException, HttpMessageNotReadableException {
        InputStream inputStream = inputMessage.getBody();
        String requestBody = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
        String method = request.getMethod();
        String uri = request.getRequestURI();
        LOGGER.debug("{} {}", method, uri);
        LOGGER.debug("{}", requestBody);
        return objectMapper.readValue(requestBody, clazz);
    }

    protected void writeInternal(Object o, HttpOutputMessage outputMessage)
            throws IOException, HttpMessageNotWritableException {
        String responseBody = objectMapper.writeValueAsString(o);
        LOGGER.debug("{}", responseBody);
        outputMessage.getBody().write(responseBody.getBytes(StandardCharsets.UTF_8));
    }
}

推荐答案

An answer from baeldung.com :

An answer from baeldung.com :

Spring 提供了一个内置的解决方案来记录有效负载.我们可以用通过插入 Spring 应用程序使用现成的过滤器配置.AbstractRequestLoggingFilter 是一个过滤器提供日志的基本功能.子类应该覆盖beforeRequest()afterRequest() 方法来执行实际记录请求.Spring框架提供以下可用于记录传入的具体实现类要求.它们是:

Spring provides a built-in solution to log payloads. We can use ready-made filters by plugging into Spring application using configuration. AbstractRequestLoggingFilter is a filter which provides basic functions of logging. Subclasses should override the beforeRequest() and afterRequest() methods to perform the actual logging around the request. Spring framework provides following concrete implementation classes which can be used to log the incoming request. These are:

Spring Boot 应用可以通过添加 bean 定义来配置启用请求日志记录:

Spring Boot application can be configured by adding a bean definition to enable request logging:

@Configuration
public class RequestLoggingFilterConfig {

    @Bean
    public CommonsRequestLoggingFilter logFilter() {
        CommonsRequestLoggingFilter filter
          = new CommonsRequestLoggingFilter();
        filter.setIncludeQueryString(true);
        filter.setIncludePayload(true);
        filter.setMaxPayloadLength(10000);
        filter.setIncludeHeaders(false);
        filter.setAfterMessagePrefix("REQUEST DATA : ");
        return filter;
    }
}

此外,此日志过滤器要求将日志级别设置为 DEBUG.在application.properties put

Also, this logging filter requires the log level be set to DEBUG. In application.properties put

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

为了使日志记录异步,我们可以使用异步附加程序.不幸的是,它不支持记录响应负载.:(

To make the logging asynchronous, we may use asynchronous appenders. Unfortunately it does not support logging response payloads. :(

这篇关于如何在 spring 休息中记录所有请求响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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