feign开启日志Logger.Level feignLoggerLevel Level爆红
skye_fly 人气:0开启日志Logger.Level feignLoggerLevel()中Level爆红
今天在学习feign时测试其配置日志的功能,发现Logger不存在Level方法,百度了一圈没有找到方法
结果自己摸索发现是导错了包,如下是错误的包
应该导入feign中的Logger包,如下
feign开启日志
配置和说明
配置
feign出于性能考虑,默认是没有日志的,需要手动开启.
package com.tuling.mall.feigndemo.config; import com.tuling.mall.feigndemo.interceptor.FeignAuthRequestInterceptor; import feign.Contract; import feign.Logger; import feign.Request; import feign.auth.BasicAuthRequestInterceptor; import feign.codec.Decoder; import feign.codec.Encoder; import feign.jackson.JacksonDecoder; import feign.jackson.JacksonEncoder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration // 注意,这个配置了@Configuration之后就是全局的bean了,就需要成了全局配置了 public class FeignConfig { /** * 日志级别 * 通过源码可以看到日志等级有 4 种,分别是: * NONE:不输出日志。 * BASIC:只输出请求方法的 URL 和响应的状态码以及接口执行的时间。 * HEADERS:将 BASIC 信息和请求头信息输出。 * FULL:输出完整的请求信息。 */ @Bean public Logger.Level feignLoggerLevel() { return Logger.Level.FULL; } }
BASIC级别
只输出请求方法的 URL 和响应的状态码以及接口执行的时间。
@Bean public Logger.Level feignLoggerLevel() { return Logger.Level.BASIC; }
当feign进行远程调用的时候控制台输出:
2022-01-12 20:39:20.537 DEBUG 30012 --- [nio-8055-exec-1] c.t.m.feigndemo.feign.OrderFeignService : [OrderFeignService#findOrderByUserId] ---> GET http://mall-order/order/findOrderByUserId/1 HTTP/1.1
2022-01-12 20:39:20.638 DEBUG 30012 --- [nio-8055-exec-1] c.t.m.feigndemo.feign.OrderFeignService : [OrderFeignService#findOrderByUserId] <--- HTTP/1.1 200 (80ms)
HEADERS
将 BASIC 信息和请求头信息输出。
@Bean public Logger.Level feignLoggerLevel() { return Logger.Level.HEADERS; }
当feign进行远程调用的时候控制台输出
2022-01-12 20:40:37.499 DEBUG 27368 --- [nio-8055-exec-5] c.t.m.feigndemo.feign.OrderFeignService : [OrderFeignService#findOrderByUserId] ---> GET http://mall-order/order/findOrderByUserId/1 HTTP/1.1
2022-01-12 20:40:37.500 DEBUG 27368 --- [nio-8055-exec-5] c.t.m.feigndemo.feign.OrderFeignService : [OrderFeignService#findOrderByUserId] Accept-Encoding: gzip
2022-01-12 20:40:37.500 DEBUG 27368 --- [nio-8055-exec-5] c.t.m.feigndemo.feign.OrderFeignService : [OrderFeignService#findOrderByUserId] Accept-Encoding: deflate
2022-01-12 20:40:37.500 DEBUG 27368 --- [nio-8055-exec-5] c.t.m.feigndemo.feign.OrderFeignService : [OrderFeignService#findOrderByUserId] Authorization: 563116cd-7124-4564-9ac5-1c740e8d70ec
2022-01-12 20:40:37.500 DEBUG 27368 --- [nio-8055-exec-5] c.t.m.feigndemo.feign.OrderFeignService : [OrderFeignService#findOrderByUserId] ---> END HTTP (0-byte body)
2022-01-12 20:40:37.553 DEBUG 27368 --- [nio-8055-exec-5] c.t.m.feigndemo.feign.OrderFeignService : [OrderFeignService#findOrderByUserId] <--- HTTP/1.1 200 (50ms)
2022-01-12 20:40:37.553 DEBUG 27368 --- [nio-8055-exec-5] c.t.m.feigndemo.feign.OrderFeignService : [OrderFeignService#findOrderByUserId] connection: keep-alive
2022-01-12 20:40:37.553 DEBUG 27368 --- [nio-8055-exec-5] c.t.m.feigndemo.feign.OrderFeignService : [OrderFeignService#findOrderByUserId] content-type: application/json
2022-01-12 20:40:37.554 DEBUG 27368 --- [nio-8055-exec-5] c.t.m.feigndemo.feign.OrderFeignService : [OrderFeignService#findOrderByUserId] date: Wed, 12 Jan 2022 12:40:37 GMT
2022-01-12 20:40:37.554 DEBUG 27368 --- [nio-8055-exec-5] c.t.m.feigndemo.feign.OrderFeignService : [OrderFeignService#findOrderByUserId] keep-alive: timeout=60
2022-01-12 20:40:37.554 DEBUG 27368 --- [nio-8055-exec-5] c.t.m.feigndemo.feign.OrderFeignService : [OrderFeignService#findOrderByUserId] transfer-encoding: chunked
2022-01-12 20:40:37.554 DEBUG 27368 --- [nio-8055-exec-5] c.t.m.feigndemo.feign.OrderFeignService : [OrderFeignService#findOrderByUserId] <--- END HTTP (100-byte body)
FULL级别
输出完整的请求信息。
@Bean public Logger.Level feignLoggerLevel() { return Logger.Level.FULL; }
当feign进行远程调用的时候控制台输出
2022-01-12 20:41:18.168 DEBUG 30232 --- [nio-8055-exec-2] c.t.m.feigndemo.feign.OrderFeignService : [OrderFeignService#findOrderByUserId] ---> GET http://mall-order/order/findOrderByUserId/1 HTTP/1.1
2022-01-12 20:41:18.169 DEBUG 30232 --- [nio-8055-exec-2] c.t.m.feigndemo.feign.OrderFeignService : [OrderFeignService#findOrderByUserId] Accept-Encoding: gzip
2022-01-12 20:41:18.169 DEBUG 30232 --- [nio-8055-exec-2] c.t.m.feigndemo.feign.OrderFeignService : [OrderFeignService#findOrderByUserId] Accept-Encoding: deflate
2022-01-12 20:41:18.169 DEBUG 30232 --- [nio-8055-exec-2] c.t.m.feigndemo.feign.OrderFeignService : [OrderFeignService#findOrderByUserId] Authorization: f4ff1ace-8aa3-4d6e-974f-83c60821210d
2022-01-12 20:41:18.169 DEBUG 30232 --- [nio-8055-exec-2] c.t.m.feigndemo.feign.OrderFeignService : [OrderFeignService#findOrderByUserId] ---> END HTTP (0-byte body)
2022-01-12 20:41:18.216 DEBUG 30232 --- [nio-8055-exec-2] c.t.m.feigndemo.feign.OrderFeignService : [OrderFeignService#findOrderByUserId] <--- HTTP/1.1 200 (45ms)
2022-01-12 20:41:18.216 DEBUG 30232 --- [nio-8055-exec-2] c.t.m.feigndemo.feign.OrderFeignService : [OrderFeignService#findOrderByUserId] connection: keep-alive
2022-01-12 20:41:18.216 DEBUG 30232 --- [nio-8055-exec-2] c.t.m.feigndemo.feign.OrderFeignService : [OrderFeignService#findOrderByUserId] content-type: application/json
2022-01-12 20:41:18.216 DEBUG 30232 --- [nio-8055-exec-2] c.t.m.feigndemo.feign.OrderFeignService : [OrderFeignService#findOrderByUserId] date: Wed, 12 Jan 2022 12:41:18 GMT
2022-01-12 20:41:18.216 DEBUG 30232 --- [nio-8055-exec-2] c.t.m.feigndemo.feign.OrderFeignService : [OrderFeignService#findOrderByUserId] keep-alive: timeout=60
2022-01-12 20:41:18.216 DEBUG 30232 --- [nio-8055-exec-2] c.t.m.feigndemo.feign.OrderFeignService : [OrderFeignService#findOrderByUserId] transfer-encoding: chunked
2022-01-12 20:41:18.216 DEBUG 30232 --- [nio-8055-exec-2] c.t.m.feigndemo.feign.OrderFeignService : [OrderFeignService#findOrderByUserId]
2022-01-12 20:41:18.217 DEBUG 30232 --- [nio-8055-exec-2] c.t.m.feigndemo.feign.OrderFeignService : [OrderFeignService#findOrderByUserId] {"msg":"success","code":0,"orders":[{"id":1,"userId":"1","commodityCode":"1","count":1,"amount":1}]}
2022-01-12 20:41:18.217 DEBUG 30232 --- [nio-8055-exec-2] c.t.m.feigndemo.feign.OrderFeignService : [OrderFeignService#findOrderByUserId] <--- END HTTP (100-byte body)
如果只想让某一个feign调用的时候有日志怎么办
在Java代码里面配置
可以用configuration = Feign2Config.class 来指定你的feign配置文件,然后在这个FeignConfig.class 配置文件里面配置日志,这样的话,只有这一个feign接口类打印日志了
需要注意的是Feign2Config不要加@Configuration注解,因为如果加了的话,就成了全局配置类了,那么所有的feign接口都会使用这个配置文件了.
@FeignClient(value = "mall-order",path = "/order",configuration = Feign2Config.class) public interface OrderFeignService { @RequestMapping("/findOrderByUserId/{userId}") R findOrderByUserId(@PathVariable("userId") Integer userId); @RequestMapping(value = "/save",consumes = MediaType.APPLICATION_JSON_VALUE) R save(@RequestBody OrderVo order);
package com.tuling.mall.feigndemo.config; import com.tuling.mall.feigndemo.interceptor.FeignAuthRequestInterceptor; import feign.Contract; import feign.Logger; import feign.Request; import feign.auth.BasicAuthRequestInterceptor; import feign.codec.Decoder; import feign.codec.Encoder; import feign.jackson.JacksonDecoder; import feign.jackson.JacksonEncoder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; //@Configuration // 注意,这个配置了@Configuration之后就是全局的bean了,就需要成了全局配置了 public class Feign2Config { /** * 日志级别 * 通过源码可以看到日志等级有 4 种,分别是: * NONE:不输出日志。 * BASIC:只输出请求方法的 URL 和响应的状态码以及接口执行的时间。 * HEADERS:将 BASIC 信息和请求头信息输出。 * FULL:输出完整的请求信息。 */ @Bean public Logger.Level feignLoggerLevel() { return Logger.Level.FULL; } }
在yaml里面配置
这样 mall-order微服务的feign日志级别就是FULL了.
feign: client: config: mall-order: #对应微服务 loggerLevel: FULL
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
加载全部内容