spring cloud netfix組件中,feign相關的日誌默認是不會輸出的,須要自定義配置才能輸出,而且Feign只對Debug基本的日誌作出響應, 實際業務須要輸出Info級別的日誌,因此須要作自定義配置,覆蓋相關配置Bean。html
Feign客戶端能夠配置各類的Logger.Level對象,告訴Feign記錄哪些日誌。Logger.Level的值有如下選擇。spring
NONE,無記錄(DEFAULT)。app
BASIC,只記錄請求方法和URL以及響應狀態代碼和執行時間。ide
HEADERS,記錄基本信息以及請求和響應標頭。ui
FULL,記錄請求和響應的頭文件,正文和元數據。this
根據Feign配置的描述,須要將Logger.Level 配置到客戶端中:spa
@Configuration public class FeignClientConfig { @Bean Logger.Level feignLoggerLevel() { return Logger.Level.FULL; } }
@FeignClient(name = "qex-comsrv", fallback = ComsrvHystrix.class, configuration = { FeignClientConfig.class }) public abstract interface ComsrvFeignApi{ @RequestMapping({"/otp/esgMsg/send.do"}) public abstract JSONObject send(OTPRequest otprequest); }
由於feign只對日誌級別爲debug級別作出響應,因此若是須要打印出日誌,還須要修改客戶端的日誌級別在application.properties中要設定一行這樣的配置:
logging.level.<你的feign client全路徑類名>: DEBUG
操做完成這三步驟,當調用Send 方法的時候就會打印出Debug級別的日誌。debug
在實際生產環境中,咱們經常須要使用Info級別日誌,使用上述針對對每一個客戶端的配置進行修改,那樣將會有大量的配置。因此,須要將修改Feign的日誌,對Info級別進行相應。日誌
public class QjxFeignLogger extends feign.Logger { private final Logger logger; public QjxFeignLogger() { this(feign.Logger.class); } public QjxFeignLogger(Class<?> clazz) { this(LoggerFactory.getLogger(clazz)); } public QjxFeignLogger(String name) { this(LoggerFactory.getLogger(name)); } QjxFeignLogger(Logger logger) { this.logger = logger; } @Override protected void logRequest(String configKey, Level logLevel, Request request) { if (logger.isInfoEnabled()) { super.logRequest(configKey, logLevel, request); } } @Override protected Response logAndRebufferResponse(String configKey, Level logLevel, Response response, long elapsedTime) throws IOException { if (logger.isInfoEnabled()) { 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.isInfoEnabled()) { logger.info(String.format(methodTag(configKey) + format, args)); } } }
自定義一個Logger類,繼承Feign.Logger,將代碼中的Debug修改爲爲Infoorm
@Configuration public class FeignClientConfig { @Bean Logger.Level feignLoggerLevel() { return Logger.Level.FULL; } @Bean Logger QjxFeign(){ return new QjxFeignLogger(); } }
將自定義Logger類加入到Feign客戶端配置的Config中,這樣Feign系統間調用就能打印出Info級別的日誌。
打印出Feign系統間調用Info級別的日誌,核心的思想是Spring Boot項目中,可以自定義配置,自定義的配置優先級大於默認的配置。詳情參見Spring Boot自定義配置。