Spring Cloud Feign(第二篇)之日誌輸出
Spring Cloud Feign 之日誌輸出
環境資訊: java 1.8、Spring boot 1.5.10.RELEASE、spring cloud-Edgware.SR3、maven 3.3+
在平時開發過程中少不了檢視日誌解決問題,一個好的框架日誌輸出是必不可少的,Feign也一樣
Feign日誌輸出說明
Feign的日誌是以下部分組成
1、Feign的Level日誌級別配置預設是:NONE,不要跟log日誌級別混淆
日誌級別列舉類 Logger.Level
NONE
不輸出日誌
BASIC
只有請求方法、URL、響應狀態程式碼、執行時間
HEADERS
FULL
請求和響應 的heads、body、metadata,建議使用這個級別
2、log日誌級別配置,預設是debug
使用指定Feign類會包名配置日誌列印級別
此處使用spring logging配置 比如列印 UserFeign logging.level.com.example.feign.UserFeign=debug
Feign日誌輸出-Logger.Level.FULL+log debug級別
全域性開啟方式 使用spring java config 配置,注意該類一定要放到spring可以掃描到的包下
/**
* feign 配置
* @author : sunshaoping
* @date: Create by in 下午4:07 2018/8/7
*/
@Configuration
public class FeignConfig {
@Bean
Logger.Level feignLevel() {
return Logger.Level.FULL;
}
}
application.properties 配置debug 日誌輸出級別
user.url=http://localhost:8080/user
logging.level.com.example.feign.UserFeign=debug
執行 UserFeignTest.save
2018-08-07 16:48:03.011 DEBUG 75661 --- [ main] com.example.feign.UserFeign : [UserFeign#save] ---> POST http://localhost:8080/user HTTP/1.1
2018-08-07 16:48:03.011 DEBUG 75661 --- [ main] com.example.feign.UserFeign : [UserFeign#save] Content-Type: application/json;charset=UTF-8
2018-08-07 16:48:03.011 DEBUG 75661 --- [ main] com.example.feign.UserFeign : [UserFeign#save] Content-Length: 27
2018-08-07 16:48:03.011 DEBUG 75661 --- [ main] com.example.feign.UserFeign : [UserFeign#save]
2018-08-07 16:48:03.011 DEBUG 75661 --- [ main] com.example.feign.UserFeign : [UserFeign#save] {"id":null,"name":"張三"}
2018-08-07 16:48:03.012 DEBUG 75661 --- [ main] com.example.feign.UserFeign : [UserFeign#save] ---> END HTTP (27-byte body)
2018-08-07 16:48:03.041 DEBUG 75661 --- [ main] com.example.feign.UserFeign : [UserFeign#save] <--- HTTP/1.1 200 (29ms)
2018-08-07 16:48:03.042 DEBUG 75661 --- [ main] com.example.feign.UserFeign : [UserFeign#save] content-length: 0
2018-08-07 16:48:03.043 DEBUG 75661 --- [ main] com.example.feign.UserFeign : [UserFeign#save] date: Tue, 07 Aug 2018 08:48:03 GMT
2018-08-07 16:48:03.043 DEBUG 75661 --- [ main] com.example.feign.UserFeign : [UserFeign#save]
2018-08-07 16:48:03.043 DEBUG 75661 --- [ main] com.example.feign.UserFeign : [UserFeign#save] <--- END HTTP (0-byte body)
執行 UserFeignTest.getUserByID
方法 可以看到以下查詢user詳情輸出的請求和響應日誌。
2018-08-07 16:48:03.045 DEBUG 75661 --- [ main] com.example.feign.UserFeign : [UserFeign#getUserByID] ---> GET http://localhost:8080/user/5 HTTP/1.1
2018-08-07 16:48:03.046 DEBUG 75661 --- [ main] com.example.feign.UserFeign : [UserFeign#getUserByID] ---> END HTTP (0-byte body)
2018-08-07 16:48:03.051 DEBUG 75661 --- [ main] com.example.feign.UserFeign : [UserFeign#getUserByID] <--- HTTP/1.1 200 (4ms)
2018-08-07 16:48:03.051 DEBUG 75661 --- [ main] com.example.feign.UserFeign : [UserFeign#getUserByID] content-type: application/json;charset=UTF-8
2018-08-07 16:48:03.051 DEBUG 75661 --- [ main] com.example.feign.UserFeign : [UserFeign#getUserByID] date: Tue, 07 Aug 2018 08:48:03 GMT
2018-08-07 16:48:03.051 DEBUG 75661 --- [ main] com.example.feign.UserFeign : [UserFeign#getUserByID] transfer-encoding: chunked
2018-08-07 16:48:03.051 DEBUG 75661 --- [ main] com.example.feign.UserFeign : [UserFeign#getUserByID]
2018-08-07 16:48:03.054 DEBUG 75661 --- [ main] com.example.feign.UserFeign : [UserFeign#getUserByID] {"id":"5","name":"張三"}
2018-08-07 16:48:03.055 DEBUG 75661 --- [ main] com.example.feign.UserFeign : [UserFeign#getUserByID] <--- END HTTP (26-byte body)
總結
此章節只介紹了feign自帶的日誌輸出配置方式,下面章節將詳細介紹其實現原理及自定義log 日誌級別輸出。
樣例地址 spring-cloud-feign 分支 Spring-Cloud-Feign之日誌輸出
,
寫在最後
Spring Cloud Feign 系列持續更新中。。。。。歡迎關注
如發現哪些知識點有誤或是沒有看懂,歡迎在評論區提出,博主及時改正。
歡迎轉載請註明出處。