将请求打印到log 的方法很多,参考了下面三个文章,用了最简单的一个方法,使用spring中内置的CommonsRequestLoggingFilter 用来打印log

新建bean

@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;
}

修改application.properties

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

参考
Spring Boot - How to log all requests and responses with exceptions in single place?
Spring – Log Incoming Requests
Logging Requests and Responses in Spring (including body)


carol_gao
88 声望1 粉丝