使用Feign logging 开启调用日志

 更新时间:2022年06月16日 10:08:49   作者:西蒙老爷  
这篇文章主要介绍了使用Feign logging 开启调用日志,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

Feign logging开启调用日志

application.yml 配置

logging:
  level:
    yourproject.userClient: debug

FeignConfiguration

@Configuration
public class FeignConfiguration {
    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }
}

效果


参考官方:http://cloud.spring.io/spring-cloud-openfeign/single/spring-cloud-openfeign.html#_feign_logging

Feign启用日志

需求

想追踪Feign客户端发送的数据.

Feign在构建被@FeignClient注解修饰的服务客户端时,会为每一个客户端都创建一个feign.Logger实例,这样就可以利用该日志对象的DEBUG模式来帮助分析Feign的请求细节。

实现步骤

1. 在application.yml中使用 logging.level.<Feign客户端对应的接口的全限定名> 的参数配置格式来开启指定客户端日志. 

logging:
  level:
    com:
      yc:
        springcloud2:
          service:
            IProductClientService: DEBUG   #注意这里是Feign客户端接口的完整路径,这是我们要监控日志的接口

2. 但由于Feign客户端默认的Logger.Level对象定义为NONE级别,因此需要对它进行调整.  Logger.Level的源码中规定了以下几个级别:

  public enum Level {
    // 不记录日志 (默认)。
    NONE,
    //只记录请求方法和URL以及响应状态代码和执行时间
    BASIC,
    //记录请求和应答的头的基本信息
    HEADERS,
    //记录请求和响应的头信息,正文和元数据
    FULL
  }

那么如何将级别从NONE修改过来呢?这里有两种方式:

1. 全局配置: 在应用主配置类中加入 Logger.Level的Bean的创建和托管,这里这个应用中所有的Feign客户端都会按这个日志级别. 

@SpringBootConfiguration
public class FeignLogConfig {
    @Bean
    Logger.Level feignLoggerLevel(){
        return Logger.Level.FULL;
    }
}

运行 程序 [microservice-consumer-feign],查看输出结果。 

2020-10-16 15:14:45.634 DEBUG 15604 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] <--- HTTP/1.1 200 (799ms)
2020-10-16 15:14:45.634 DEBUG 15604 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] cache-control: no-cache, no-store, max-age=0, must-revalidate
2020-10-16 15:14:45.634 DEBUG 15604 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] connection: keep-alive
2020-10-16 15:14:45.634 DEBUG 15604 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] content-type: application/json;charset=UTF-8
2020-10-16 15:14:45.634 DEBUG 15604 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] date: Fri, 16 Oct 2020 07:14:45 GMT
2020-10-16 15:14:45.634 DEBUG 15604 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] expires: 0
2020-10-16 15:14:45.634 DEBUG 15604 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] keep-alive: timeout=60
2020-10-16 15:14:45.634 DEBUG 15604 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] pragma: no-cache
2020-10-16 15:14:45.634 DEBUG 15604 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] transfer-encoding: chunked
2020-10-16 15:14:45.634 DEBUG 15604 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] x-content-type-options: nosniff
2020-10-16 15:14:45.634 DEBUG 15604 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] x-frame-options: DENY
2020-10-16 15:14:45.634 DEBUG 15604 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] x-xss-protection: 1; mode=block
2020-10-16 15:14:45.635 DEBUG 15604 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] 
2020-10-16 15:14:45.636 DEBUG 15604 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] [{"productId":1,"productName":"a","productDesc":"good"}]
2020-10-16 15:14:45.637 DEBUG 15604 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] <--- END HTTP (56-byte body)

2. 针对一个具体的Feign客户端单独配置.  比如这里对一个Feign客户端进行日志配置. 

@Configuration
public class FeignClientConfig {
    //加入安全配置
    @Bean
    public BasicAuthRequestInterceptor getBasicAuthRequestInterceptor(){
        return new BasicAuthRequestInterceptor("admin","a");
    }
 
    @Bean
    Logger.Level feignLoggerLevel(){
        return Logger.Level.FULL;
    }
}

重启应用程序,查看输出:

2020-10-16 15:20:21.856 DEBUG 15620 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] <--- HTTP/1.1 200 (476ms)
2020-10-16 15:20:21.856 DEBUG 15620 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] cache-control: no-cache, no-store, max-age=0, must-revalidate
2020-10-16 15:20:21.856 DEBUG 15620 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] connection: keep-alive
2020-10-16 15:20:21.856 DEBUG 15620 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] content-type: application/json;charset=UTF-8
2020-10-16 15:20:21.856 DEBUG 15620 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] date: Fri, 16 Oct 2020 07:20:21 GMT
2020-10-16 15:20:21.856 DEBUG 15620 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] expires: 0
2020-10-16 15:20:21.856 DEBUG 15620 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] keep-alive: timeout=60
2020-10-16 15:20:21.856 DEBUG 15620 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] pragma: no-cache
2020-10-16 15:20:21.856 DEBUG 15620 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] transfer-encoding: chunked
2020-10-16 15:20:21.857 DEBUG 15620 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] x-content-type-options: nosniff
2020-10-16 15:20:21.857 DEBUG 15620 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] x-frame-options: DENY
2020-10-16 15:20:21.857 DEBUG 15620 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] x-xss-protection: 1; mode=block
2020-10-16 15:20:21.857 DEBUG 15620 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] 
2020-10-16 15:20:21.859 DEBUG 15620 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] [{"productId":1,"productName":"a","productDesc":"good"}]
2020-10-16 15:20:21.859 DEBUG 15620 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] <--- END HTTP (56-byte body)

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • kettle中使用js调用java类的方法

    kettle中使用js调用java类的方法

    这篇文章主要介绍了kettle中使用js调用java类的方法,本文讲解了注意事项和调用语法,需要的朋友可以参考下
    2015-05-05
  • Spring切入点表达式配置过程图解

    Spring切入点表达式配置过程图解

    这篇文章主要介绍了Spring切入点表达式配置过程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-08-08
  • Java 导出 CSV 文件操作详情

    Java 导出 CSV 文件操作详情

    这篇文章主要介绍了Java导出CSV文件操作详情,文章通过导入坐标展开详细内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-08-08
  • 详解SpringBoot中@NotNull,@NotBlank注解使用

    详解SpringBoot中@NotNull,@NotBlank注解使用

    这篇文章主要为大家详细介绍了Spring Boot中集成Validation与@NotNull,@NotBlank等注解的简单使用,感兴趣的小伙伴可以跟随小编一起学习一下
    2022-08-08
  • SpringBoot中的Actuator详解

    SpringBoot中的Actuator详解

    这篇文章主要介绍了SpringBoot中的Actuator详解,Spring Boot Actuator 在Spring Boot第一个版本发布的时候就有了,它为Spring Boot提供了一系列产品级的特性,监控应用程序,收集元数据,运行情况或者数据库状态等,需要的朋友可以参考下
    2023-09-09
  • java 判断list是否为空过程解析

    java 判断list是否为空过程解析

    这篇文章主要介绍了java 判断list是否为空过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09
  • 解决mybatis 执行mapper的方法时报空指针问题

    解决mybatis 执行mapper的方法时报空指针问题

    这篇文章主要介绍了解决mybatis 执行mapper的方法时报空指针问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • FastJSON的0day漏洞的解决

    FastJSON的0day漏洞的解决

    本文主要介绍了FastJSON的0day漏洞的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-05-05
  • Java使用Soap方式调用WebService接口代码示例

    Java使用Soap方式调用WebService接口代码示例

    Java调用WebService接口是指通过Java语言来访问并与WebService进行交互,WebService是一种基于Web的服务架构,它通过标准的XML和HTTP协议来提供服务,这篇文章主要给大家介绍了关于Java使用Soap方式调用WebService接口的相关资料,需要的朋友可以参考下
    2024-03-03
  • java同步之volatile解析

    java同步之volatile解析

    volatile可以说是Java虚拟机提供的最轻量级的同步机制了,了解volatile的语义对理解多线程的特性具有很重要的意义,下面小编带大家一起学习一下
    2019-05-05

最新评论