您好,登錄后才能下訂單哦!
這篇文章主要講解了“如何使用Feign logging開啟調用日志”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“如何使用Feign logging開啟調用日志”吧!
application.yml 配置
logging: level: yourproject.userClient: debug
FeignConfiguration
@Configuration public class FeignConfiguration { @Bean Logger.Level feignLoggerLevel() { return Logger.Level.FULL; } }
效果
想追蹤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)
感謝各位的閱讀,以上就是“如何使用Feign logging開啟調用日志”的內容了,經過本文的學習后,相信大家對如何使用Feign logging開啟調用日志這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。