使用Feign?logging?開啟調(diào)用日志
Feign logging開啟調(diào)用日志
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客戶端發(fā)送的數(shù)據(jù).
Feign在構(gòu)建被@FeignClient注解修飾的服務客戶端時,會為每一個客戶端都創(chuàng)建一個feign.Logger實例,這樣就可以利用該日志對象的DEBUG模式來幫助分析Feign的請求細節(jié)。
實現(xiàn)步驟
1. 在application.yml中使用 logging.level.<Feign客戶端對應的接口的全限定名> 的參數(shù)配置格式來開啟指定客戶端日志.
logging: ? level: ? ? com: ? ? ? yc: ? ? ? ? springcloud2: ? ? ? ? ? service: ? ? ? ? ? ? IProductClientService: DEBUG ? #注意這里是Feign客戶端接口的完整路徑,這是我們要監(jiān)控日志的接口
2. 但由于Feign客戶端默認的Logger.Level對象定義為NONE級別,因此需要對它進行調(diào)整. Logger.Level的源碼中規(guī)定了以下幾個級別:
public enum Level { // 不記錄日志 (默認)。 NONE, //只記錄請求方法和URL以及響應狀態(tài)代碼和執(zhí)行時間 BASIC, //記錄請求和應答的頭的基本信息 HEADERS, //記錄請求和響應的頭信息,正文和元數(shù)據(jù) FULL }
那么如何將級別從NONE修改過來呢?這里有兩種方式:
1. 全局配置: 在應用主配置類中加入 Logger.Level的Bean的創(chuàng)建和托管,這里這個應用中所有的Feign客戶端都會按這個日志級別.
@SpringBootConfiguration public class FeignLogConfig { @Bean Logger.Level feignLoggerLevel(){ return Logger.Level.FULL; } }
運行 程序 [microservice-consumer-feign],查看輸出結(jié)果。
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)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
詳解SpringBoot中@NotNull,@NotBlank注解使用
這篇文章主要為大家詳細介紹了Spring?Boot中集成Validation與@NotNull,@NotBlank等注解的簡單使用,感興趣的小伙伴可以跟隨小編一起學習一下2022-08-08解決mybatis 執(zhí)行mapper的方法時報空指針問題
這篇文章主要介紹了解決mybatis 執(zhí)行mapper的方法時報空指針問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07Java使用Soap方式調(diào)用WebService接口代碼示例
Java調(diào)用WebService接口是指通過Java語言來訪問并與WebService進行交互,WebService是一種基于Web的服務架構(gòu),它通過標準的XML和HTTP協(xié)議來提供服務,這篇文章主要給大家介紹了關于Java使用Soap方式調(diào)用WebService接口的相關資料,需要的朋友可以參考下2024-03-03