使用Feign logging 开启调用日志

网友投稿 383 2022-07-23


目录Feign logging开启调用日志Feign启用日志需求实现步骤

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. 的参数配置格式来开启指定客户端日志.

logging:

level:

com:

yc:

springcloud2:

service:

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

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

public enum Level {

// 不记录日志 (默认)。

NONE,

//bDzwhVYt只记录请求方法和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-revalidate2020-10-16 15:14:45.634 DEBUG 15604 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] connection: keep-alive2020-10-16 15:14:45.634 DEBUG 15604 --- [nio-9999-exec-1] c.bDzwhVYty.s.service.IProductClientService      : [IProductClientService#listProduct] content-type: application/json;charset=UTF-82020-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 GMT2020-10-16 15:14:45.634 DEBUG 15604 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] expires: 02020-10-16 15:14:45.634 DEBUG 15604 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] keep-alive: timeout=602020-10-16 15:14:45.634 DEBUG 15604 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] pragma: no-cache2020-10-16 15:14:45.634 DEBUG 15604 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] transfer-encoding: chunked2020-10-16 15:14:45.634 DEBUG 15604 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] x-content-type-options: nosniff2020-10-16 15:14:45.634 DEBUG 15604 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] x-frame-options: DENY2020-10-16 15:14:45.634 DEBUG 15604 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] x-xss-protection: 1; mode=block2020-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-revalidate2020-10-16 15:20:21.856 DEBUG 15620 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] connection: keep-alive2020-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-82020-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 GMT2020-10-16 15:20:21.856 DEBUG 15620 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] expires: 02020-10-16 15:20:21.856 DEBUG 15620 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] keep-alive: timeout=602020-10-16 15:20:21.856 DEBUG 15620 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] pragma: no-cache2020-10-16 15:20:21.856 DEBUG 15620 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] transfer-encoding: chunked2020-10-16 15:20:21.857 DEBUG 15620 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] x-content-type-options: nosniff2020-10-16 15:20:21.857 DEBUG 15620 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] x-frame-options: DENY2020-10-16 15:20:21.857 DEBUG 15620 --- [nio-9999-exec-1] c.y.s.service.IProductClientService      : [IProductClientService#listProduct] x-xss-protection: 1; mode=block2020-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)


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:一文搞懂Java项目中枚举的定义与使用
下一篇:全局记录Feign的请求和响应日志方式
相关文章

 发表评论

暂时没有评论,来抢沙发吧~