SpringBoot配置FeignClient打印请求日志

一、Feign原生方式配置(原文:https://blog.csdn.net/cen776727328/article/details/101273190

1、创建Feign的配置文件,并在其中设置日志等级

@Configuration
public class FeignConfiguration {
    @Bean
    Logger.Level feignLoggerLevel() {
        //这里记录所有,根据实际情况选择合适的日志level
        return Logger.Level.FULL;
    }
}

 2、在客户端接口指定此配置 


@FeignClient(value = "haoait-auth", configuration = FeignConfiguration.class, fallback = *****.class)
public interface UserServiceClient extends UserService {
 

3、配置文件开启日志记录
application.properties设置:
logging.level.com.haoait.client.UserServiceClient:debug


logging:
  level:
    com.haoait.client.UserServiceClient:debug

二、spring 代理

1. 在 application.yml(bootstrap.yml)中配置(一 、1 中也可以这样配置)

feign:
  okhttp: 
    enabled: false
  httpclient:
    #Apache的HTTP Client替换Feign原始的http client
    enabled: true
    max-connections: 20480
    max-connections-per-route: 512
    time-to-live: 60
    time-to-live-unit: SECONDS
    connection-timeout: 60000
    user-agent: 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0'
  client:
    # 配置文件优先级高
    default-to-properties: true
    config:
      default:
        logger-level: BASIC
        connect-timeout: 60000
        read-timeout: 60000
        decode404: true

也可以代码里写 

public class FeignConfig {

    /**
     * feign 的日志记录
     * @return
     */
    @Bean
    Logger.Level feignLoggerLevel() {
        /* NONE, 不记录日志 (默认)。
        BASIC, 只记录请求方法和URL以及响应状态代码和执行时间。
        HEADERS, 记录请求和应答的头的基本信息。
        FULL, 记录请求和响应的头信息,正文和元数据。*/
        log.debug("feign输出full级别日志:[ 记录请求和响应的头信息,正文和元数据]");
        return Logger.Level.FULL;
    }

}

2. 配置feignconfig

   增加了几个非常有用的其他配置项

@Configuration(proxyBeanMethods = false)
@AutoConfigureAfter({HttpClientFeignConfiguration.class})
@AutoConfigureBefore({FeignClientsConfiguration.class})
public class FeignConfig {
    
 /**
     * +使用Apache Httpclient实现FeignClient客户端
     * @param httpClient #HttpClientFeignConfiguration.customHttpClient()
     * @return ApacheHttpClient实例
     */
    @Bean
    @Primary
    public Client feignClient(HttpClient httpClient) {
        return new ApacheHttpClient(httpClient);
    }
/**
     * +用Spring定义的日志系统代理feign日志系统
     * @return 代理日志系统
     */
    @Bean
    @Primary
    public Logger logger() {
        return new FeignLog(this.getClass());
    }

 /**
     * Springboot的代理log
    
     *
     */
    final class FeignLog extends Logger {
        private Log log;

        public FeignLog(Class<?> clazz) {
            log = LogFactory.getLog(clazz);
        }

        @Override
        protected void log(String configKey, String format, Object... args) {
            //if (log.isDebugEnabled()) {
                log.info(String.format(methodTag(configKey) + format, args));
            //}
        }
    }
/**
     * +重试机制,任何情况下都不重试
     * @return 禁止重试机制
     */
    @Bean
    @Primary
    public Retryer feignRetryer() {
        return Retryer.NEVER_RETRY;
    }

 3. 在客户端接口指定此配置

主程序运行类添加 @EnableFeignClients(basePackages = {"com.****.feign"})

@FeignClient(name = "aaaFeignClient",
        url = "${url}",
        fallback = AAAFeignClientFallback.class,
        configuration = {FeignConfig.class})
public interface AAAFeignClient {

4.输出结果

猜你喜欢

转载自blog.csdn.net/yunxing323/article/details/111659310