Feign輸出Info級別日志


背景

  spring cloud netfix組件中,feign相關的日志默認是不會輸出的,需要自定義配置才能輸出,並且Feign只對Debug基本的日志做出響應, 實際業務需要輸出Info級別的日志,所以需要做自定義配置,覆蓋相關配置Bean。

Feign配置

  Feign客戶端可以配置各種的Logger.Level對象,告訴Feign記錄哪些日志。Logger.Level的值有以下選擇。

 

    NONE,無記錄(DEFAULT)。

 

    BASIC,只記錄請求方法和URL以及響應狀態代碼和執行時間。

 

    HEADERS,記錄基本信息以及請求和響應標頭。

 

    FULL,記錄請求和響應的頭文件,正文和元數據。

打印Feign日志

  1、Feign的配置類

    根據Feign配置的描述,需要將Logger.Level 配置到客戶端中:

 @Configuration
  public class FeignClientConfig {
          @Bean
          Logger.Level feignLoggerLevel() {
                  return Logger.Level.FULL;
          }     
  }

  2、在客戶端中修改@FeignClient注解

 

  @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);
        
  }

  3、修改客戶端的日志打印級別

    因為feign只對日志級別為debug級別做出響應,所以如果需要打印出日志,還需要修改客戶端的日志級別在application.properties中要設定一行這樣的配置:
        logging.level.<你的feign client全路徑類名>: DEBUG
    操作完成這三步驟,當調用Send 方法的時候就會打印出Debug級別的日志。

打印Info級別日志

  在實際生產環境中,我們常常需要使用Info級別日志,使用上述針對對每個客戶端的配置進行修改,那樣將會有大量的配置。所以,需要將修改Feign的日志,對Info級別進行相應。

  1、重寫feign.logger類

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修改成為Info

  2、自定義Logger類加入配置

 @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自定義配置

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM