Fork me on GitHub

Spring Cloud Feign 日志设置

由于FeignClient封装了restful请求,我们很难看出发出的请求和收到的响应具体是什么。

为此我们可以做一些设置来打印出相应的日志。

日志配置

  • 第一步,编写FeignClient的configuration类。
1
2
3
4
5
6
7
@Configuration
public class FeignLogConfiguration {
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
1
2
3
4
5
@FeignClient(name = "microservice-provider-user", configuration = FeignLogConfiguration.class)
public interface UserFeignClient {
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public User findById(@PathVariable("id") Long id);
}

Feign的日志级别枚举如下:

  • 第二步,配置FeignClient所在包的日志级别为DEBUG
1
2
3
4
# application.yml
logging:
level:
com.cloud.study.feign: DEBUG

如果你不是在application.yml中配置的日志级别,而是使用logback-spring.xml,同理,在logback-spring.xml中做相应配置:

1
2
3
4
# logback-spring.xml
<logger name="com.cloud.study.feign" level="DEBUG" additivity="false">
<appender-ref ref="Console"/>
</logger>

日志输出

当你配置完以上之后,每次FeignClient的调用都会打印出详细日志,具体如下:

求鼓励,求支持!