使用Feign?logging?開(kāi)啟調(diào)用日志
Feign logging開(kāi)啟調(diào)用日志
application.yml 配置
logging:
level:
yourproject.userClient: debugFeignConfiguration
@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注解修飾的服務(wù)客戶端時(shí),會(huì)為每一個(gè)客戶端都創(chuàng)建一個(gè)feign.Logger實(shí)例,這樣就可以利用該日志對(duì)象的DEBUG模式來(lái)幫助分析Feign的請(qǐng)求細(xì)節(jié)。
實(shí)現(xiàn)步驟
1. 在application.yml中使用 logging.level.<Feign客戶端對(duì)應(yīng)的接口的全限定名> 的參數(shù)配置格式來(lái)開(kāi)啟指定客戶端日志.
logging: ? level: ? ? com: ? ? ? yc: ? ? ? ? springcloud2: ? ? ? ? ? service: ? ? ? ? ? ? IProductClientService: DEBUG ? #注意這里是Feign客戶端接口的完整路徑,這是我們要監(jiān)控日志的接口
2. 但由于Feign客戶端默認(rèn)的Logger.Level對(duì)象定義為NONE級(jí)別,因此需要對(duì)它進(jìn)行調(diào)整. Logger.Level的源碼中規(guī)定了以下幾個(gè)級(jí)別:
public enum Level {
// 不記錄日志 (默認(rèn))。
NONE,
//只記錄請(qǐng)求方法和URL以及響應(yīng)狀態(tài)代碼和執(zhí)行時(shí)間
BASIC,
//記錄請(qǐng)求和應(yīng)答的頭的基本信息
HEADERS,
//記錄請(qǐng)求和響應(yīng)的頭信息,正文和元數(shù)據(jù)
FULL
}那么如何將級(jí)別從NONE修改過(guò)來(lái)呢?這里有兩種方式:
1. 全局配置: 在應(yīng)用主配置類中加入 Logger.Level的Bean的創(chuàng)建和托管,這里這個(gè)應(yīng)用中所有的Feign客戶端都會(huì)按這個(gè)日志級(jí)別.
@SpringBootConfiguration
public class FeignLogConfig {
@Bean
Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
}
}
運(yùn)行 程序 [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. 針對(duì)一個(gè)具體的Feign客戶端單獨(dú)配置. 比如這里對(duì)一個(gè)Feign客戶端進(jìn)行日志配置.
@Configuration
public class FeignClientConfig {
//加入安全配置
@Bean
public BasicAuthRequestInterceptor getBasicAuthRequestInterceptor(){
return new BasicAuthRequestInterceptor("admin","a");
}
@Bean
Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
}
}
重啟應(yīng)用程序,查看輸出:
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)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
從源碼角度看spring mvc的請(qǐng)求處理過(guò)程
這篇文章主要介紹了從源碼角度看spring mvc的請(qǐng)求處理過(guò)程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,,需要的朋友可以參考下2019-06-06
Socket結(jié)合線程池使用實(shí)現(xiàn)客戶端和服務(wù)端通信demo
這篇文章主要為大家介紹了Socket結(jié)合線程池的使用來(lái)實(shí)現(xiàn)客戶端和服務(wù)端通信實(shí)戰(zhàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
Spring?cloud網(wǎng)關(guān)gateway進(jìn)行websocket路由轉(zhuǎn)發(fā)規(guī)則配置過(guò)程
這篇文章主要介紹了Spring?cloud網(wǎng)關(guān)gateway進(jìn)行websocket路由轉(zhuǎn)發(fā)規(guī)則配置過(guò)程,文中還通過(guò)實(shí)例代碼介紹了Spring?Cloud?Gateway--配置路由的方法,需要的朋友可以參考下2023-04-04
Maven中Junit測(cè)試@Test等注解無(wú)法識(shí)別的問(wèn)題及解決
這篇文章主要介紹了Maven中Junit測(cè)試@Test等注解無(wú)法識(shí)別的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
Java中Hashtable類與HashMap類的區(qū)別詳解
Hashtable的應(yīng)用非常廣泛,HashMap是新框架中用來(lái)代替Hashtable的類,也就是說(shuō)建議使用HashMap,不要使用Hashtable??赡苣阌X(jué)得Hashtable很好用,為什么不用呢?這里簡(jiǎn)單分析他們的區(qū)別。2016-01-01

