0

我想在我的应用程序中实现 CommonsRequestLoggingFilter 并将请求和响应获取到 Java 代码中:

@Bean
public CommonsRequestLoggingFilter requestLoggingFilter() {
    CommonsRequestLoggingFilter loggingFilter = new CommonsRequestLoggingFilter();
    loggingFilter.setIncludeClientInfo(true);
    loggingFilter.setIncludeQueryString(true);
    loggingFilter.setIncludePayload(true);
    loggingFilter.setIncludeHeaders(false);
    return loggingFilter;
}

如何将有效负载放入 Rest 控制器代码?例子:

@RestController
public class LoggingDemoController {

    @GetMapping("/demo/greeting")
    public String sayHello(){

        // Get here the payload from CommonsRequestLoggingFilter

        return "Hello Stranger !!!";
    }
}

这可能吗?

4

1 回答 1

-1

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

@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

<logger name="org.springframework.web.filter.CommonsRequestLoggingFilter">
    <level value="DEBUG" />
</logger>
于 2019-01-17T09:27:40.800 回答