OpenFeign提供了日志打印功能,我们可以通过配置来调整日志级别,从而了解OpenFeign中Http请求的细节。说白了就是对OpenFeign接口的调用情况进行监控和输出

1.日志级别

级别名称 描述
NONE 默认的,不显示任何日志
BASIC 仅记录请求方法、URL、响应状态码及执行时间
HEADERS 除了BASIC中定义的信息之外,还请求和响应的头信息
FULL 出了BASIC中定义的信息之外,还有请求和响应的正文及元数据

2.配置日志JavaBean


在cloud-consumer-feign-order80项目中添加JavaBean配置

  1. @Configuration
  2. public class FeignConfig {
  3. @Bean
  4. Logger.Level feignLoggerLevel(){
  5. return Logger.Level.FULL;
  6. }
  7. }

3.修改application.yml配置文件


修改cloud-consumer-feign-order80项目中的application.yml配置文件增加如下内容

logging:
  level:
    com.springcloud.service.PaymentFeignService: debug

4.启动cloud-consumer-feign-order80项目


浏览器输入:http://localhost/consumer/payment/get/1
观察cloud-consumer-feign-order80项目日志打印,应该会返回如下内容

[PaymentFeignService#getPaymentById] ---> GET http://CLOUD-PAYMENT-SERVICE/payment/get/1 HTTP/1.1
[PaymentFeignService#getPaymentById] ---> END HTTP (0-byte body)
[PaymentFeignService#getPaymentById] <--- HTTP/1.1 200 (2019ms)
[PaymentFeignService#getPaymentById] connection: keep-alive
[PaymentFeignService#getPaymentById] content-type: application/json
[PaymentFeignService#getPaymentById] date: Thu, 19 Mar 2020 13:54:51 GMT
[PaymentFeignService#getPaymentById] keep-alive: timeout=60
[PaymentFeignService#getPaymentById] transfer-encoding: chunked
[PaymentFeignService#getPaymentById] 
[PaymentFeignService#getPaymentById] {"code":200,"message":"查询成功,serverPort: 8001","data":{"id":1,"serial":"哈哈哈"}}
[PaymentFeignService#getPaymentById] <--- END HTTP (91-byte body)