Spring Cloud Feign 之日志自定義擴(kuò)展
遷移到CSDN
環(huán)境信息: java 1.8奶陈、Spring boot 1.5.10.RELEASE简软、spring cloud-Edgware.SR3女仰、maven 3.3+
第二章 Spring Cloud Feign 之日志輸出已經(jīng)對(duì)Feign自帶的日志輸出說(shuō)明智听,與外部HTTP接口交互時(shí)需要記錄一些請(qǐng)求和響應(yīng)日志來(lái)排查問(wèn)題,雖然Feign支持但它的日志是Debug級(jí)別怜珍,并不符合我們?cè)谏a(chǎn)中使用INFO級(jí)別日志要求端蛆,所以這章介紹下自定義日志輸出。
分析Spring Cloud Feign 默認(rèn)日志
首先看下spring cloud feign 對(duì)日志輸出的處理
package org.springframework.cloud.netflix.feign;
import feign.Logger;
/**
* Allows an application to use a custom Feign {@link Logger}.
*
* @author Venil Noronha
*/
public interface FeignLoggerFactory {
/**
* Factory method to provide a {@link Logger} for a given {@link Class}.
*
* @param type the {@link Class} for which a {@link Logger} instance is to be created
* @return a {@link Logger} instance
*/
public Logger create(Class<?> type);
}
通過(guò)源碼我們知道spring cloud feign 已經(jīng)對(duì)feign的日志輸出這塊做了擴(kuò)展酥泛,當(dāng)然feign本身也可以今豆,這里針對(duì)spring cloud feign 進(jìn)行講解。
FeignLoggerFactory
是Feign日志工廠接口類(lèi)柔袁,DefaultFeignLoggerFactory
是它的默認(rèn)實(shí)現(xiàn)呆躲,spring cloud feign 就是用的 DefaultFeignLoggerFactory
@Override
public Logger create(Class<?> type) {
return this.logger != null ? this.logger : new Slf4jLogger(type);
}
默認(rèn)日志工廠使用Slf4jLogger
日志,然后我們看下源碼
package feign.slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import feign.Request;
import feign.Response;
public class Slf4jLogger extends feign.Logger {
private final Logger logger;
public Slf4jLogger() {
this(feign.Logger.class);
}
public Slf4jLogger(Class<?> clazz) {
this(LoggerFactory.getLogger(clazz));
}
public Slf4jLogger(String name) {
this(LoggerFactory.getLogger(name));
}
Slf4jLogger(Logger logger) {
this.logger = logger;
}
@Override
protected void logRequest(String configKey, Level logLevel, Request request) {
if (logger.isDebugEnabled()) {
super.logRequest(configKey, logLevel, request);
}
}
@Override
protected Response logAndRebufferResponse(String configKey, Level logLevel,
Response response,long elapsedTime) throws IOException {
if (logger.isDebugEnabled()) {
return super.logAndRebufferResponse(configKey, logLevel, response, elapsedTime);
}
return response;
}
@Override
protected void log(String configKey, String format, Object... args) {
// Not using SLF4J's support for parameterized messages (even though it would be more efficient) because it would
// require the incoming message formats to be SLF4J-specific.
if (logger.isDebugEnabled()) {
logger.debug(String.format(methodTag(configKey) + format, args));
}
}
}
看到這里就知道為什么Spring Cloud Feign 日志輸出的是Debug級(jí)別日志了捶索。
自定Spring Cloud Feign日志輸出
參考DefaultFeignLoggerFactory
類(lèi)實(shí)現(xiàn)自己的日志工廠實(shí)現(xiàn)類(lèi)插掂。
場(chǎng)景說(shuō)明:將原有的debug級(jí)別,修改成info級(jí)別
第一步:實(shí)現(xiàn)FeignLoggerFactory
工廠接口,InfoFeignLoggerFactory
是FeignConfig
靜態(tài)內(nèi)部類(lèi)
/**
* feign info 日志工廠
*/
public static class InfoFeignLoggerFactory implements FeignLoggerFactory {
@Override
public Logger create(Class<?> type) {
return new InfoFeignLogger(LoggerFactory.getLogger(type));
}
}
第二部: 繼承feign.Logger
實(shí)現(xiàn)info級(jí)別日志輸出腥例,InfoFeignLogger
使用slf4j
日志工具辅甥,此處只是簡(jiǎn)單的實(shí)現(xiàn)了info級(jí)別日志輸出,如果想效率更高請(qǐng)參考Slf4jLogger
添加一些日記級(jí)別判斷
/**
* info feign 日志
* @author: sunshaoping
* @date: Create by in 下午3:29 2018/8/8
*/
public class InfoFeignLogger extends feign.Logger {
private final Logger logger;
public InfoFeignLogger(Logger logger) {
this.logger = logger;
}
@Override
protected void log(String configKey, String format, Object... args) {
if (logger.isInfoEnabled()) {
logger.info(String.format(methodTag(configKey) + format, args));
}
}
}
第三部:日志工廠InfoFeignLoggerFactory
注冊(cè)到spring 容器中燎竖,使用spring java Config
/**
* feign 配置
* @author: sunshaoping
* @date: Create by in 下午4:07 2018/8/7
*/
@Configuration
public class FeignConfig {
@Bean
Logger.Level feignLevel() {
return Logger.Level.FULL;
}
@Bean
FeignLoggerFactory infoFeignLoggerFactory() {
return new InfoFeignLoggerFactory();
}
}
這樣就實(shí)現(xiàn)了自定義的日志打印了璃弄,簡(jiǎn)單吧,讓我們看看效果构回,是不是變成INFO日志級(jí)別了夏块。
2018-08-08 15:45:39.261 INFO 3514 --- [ main] com.example.feign.UserFeign : [UserFeign#save] ---> POST http://localhost:8080/user HTTP/1.1
2018-08-08 15:45:39.262 INFO 3514 --- [ main] com.example.feign.UserFeign : [UserFeign#save] Content-Type: application/json;charset=UTF-8
2018-08-08 15:45:39.262 INFO 3514 --- [ main] com.example.feign.UserFeign : [UserFeign#save] Content-Length: 27
2018-08-08 15:45:39.262 INFO 3514 --- [ main] com.example.feign.UserFeign : [UserFeign#save]
2018-08-08 15:45:39.262 INFO 3514 --- [ main] com.example.feign.UserFeign : [UserFeign#save] {"id":null,"name":"張三"}
2018-08-08 15:45:39.263 INFO 3514 --- [ main] com.example.feign.UserFeign : [UserFeign#save] ---> END HTTP (27-byte body)
2018-08-08 15:45:39.691 INFO 3514 --- [ main] com.example.feign.UserFeign : [UserFeign#save] <--- HTTP/1.1 200 (427ms)
2018-08-08 15:45:39.691 INFO 3514 --- [ main] com.example.feign.UserFeign : [UserFeign#save] content-length: 0
2018-08-08 15:45:39.691 INFO 3514 --- [ main] com.example.feign.UserFeign : [UserFeign#save] date: Wed, 08 Aug 2018 07:45:39 GMT
2018-08-08 15:45:39.691 INFO 3514 --- [ main] com.example.feign.UserFeign : [UserFeign#save]
2018-08-08 15:45:39.692 INFO 3514 --- [ main] com.example.feign.UserFeign : [UserFeign#save] <--- END HTTP (0-byte body)
細(xì)心的同學(xué)可能會(huì)發(fā)現(xiàn),為什么我們的FeignLoggerFactory
就起作用了纤掸,其實(shí)這是spring 強(qiáng)大的條件裝配功能
當(dāng)FeignLoggerFactory
不存在時(shí)才加載默認(rèn)DefaultFeignLoggerFactory
,如果對(duì)spring 條件裝配感興趣的同學(xué)可以看下官網(wǎng)脐供,spring boot對(duì)spring條件裝配擴(kuò)展了很多注解。
@Bean
@ConditionalOnMissingBean(FeignLoggerFactory.class)
public FeignLoggerFactory feignLoggerFactory() {
return new DefaultFeignLoggerFactory(logger);
}
總結(jié)
此章節(jié)介紹了自定義Feign 日志輸出借跪,通過(guò)實(shí)現(xiàn)FeignLoggerFactory
工廠類(lèi)接口和繼承feign.Logger
類(lèi)政己,還使用了 slf4j
日志工具,在項(xiàng)目開(kāi)發(fā)過(guò)程中建議使用slf4j
這樣項(xiàng)目在更換日志框架也不用修改源代碼了垦梆,擴(kuò)展性更強(qiáng)匹颤。
樣例地址 spring-cloud-feign 分支 Spring-Cloud-Feign之日志自定義擴(kuò)展
,
寫(xiě)在最后
Spring Cloud Feign 系列持續(xù)更新中托猩。印蓖。。京腥。赦肃。歡迎關(guān)注
如發(fā)現(xiàn)哪些知識(shí)點(diǎn)有誤或是沒(méi)有看懂,歡迎在評(píng)論區(qū)提出公浪,博主及時(shí)改正他宛。
歡迎轉(zhuǎn)載請(qǐng)注明出處。