设置feign配置日志级别

设置feign配置日志级别

一、使用java编码的方式

基于《<spring-cloud.version>Greenwich.RC2</spring-cloud.version>》

根据spring cloud文档: https://cloud.spring.io/spring-cloud-static/Greenwich.RC2/single/spring-cloud.html#_feign_logging

feign日志
在创建feign client的时候,就创建了logger, 默认logger的名称是创建feign client的服务接口类的全路径,通俗的来讲就是加了@FeignClient接口类的全路径

首先在application.yml文件中指定日志级别:

logging:
  level:
    com.**.api.service.IFeignService: DEBUG

设置日志记录的级别
logger有四种类型:NONE,BASIC,HEADERS, FULL,通过注册Bean来设置日志记录级别:

@Configuration
public class LogConfiguration {

    @Bean
    Logger.Level feignLoggerLevel(){
        return Logger.Level.FULL;
    }

}

在feign client加上:

@FeignClient(name = "IFeignService", configuration = LogConfiguration.class)
public interface IFeignService {

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String invokeSayHi();

}

日志记录结果:

2018-12-31    c.g.v.f.c.api.service.IFeignService  <--- HTTP/1.1 200 (462ms)
2018-12-31    c.g.v.f.c.api.service.IFeignService  content-length: 5
2018-12-31    c.g.v.f.c.api.service.IFeignService  content-type: text/plain;charset=UTF-8
2018-12-31    c.g.v.f.c.api.service.IFeignService  date: Mon, 31 Dec 2018 13:35:48 GMT
2018-12-31    c.g.v.f.c.api.service.IFeignService 
2018-12-31    c.g.v.f.c.api.service.IFeignService  index
2018-12-31    c.g.v.f.c.api.service.IFeignService  <--- END HTTP (5-byte body)

内容参考
https://blog.csdn.net/u013887008/article/details/85489631

缺点:

如上方法,针对每一个有@FeignClient注解的地方都要增加configuration配置,项目大了,不好到处修改。

二、使用配置文件的方式

在application.yml文件中全局配置

logging:
  level:
#    com.maybesuch.contentcenter.feignclient.UserCenterFeignClient: debug
    com.maybesuch: debug

feign:
  client:
    config:
      # 要调用服务的名称
      user-center:
        loggerLevel: full

缺点同上,有多少个调用服务名称,就需要配置多个。
参考https://www.cnblogs.com/maybesuch/p/12175002.html

重写feign.logger类,并加入配置

则可以使用如下方法
比如打印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级别的日志。

转载自 https://www.cnblogs.com/lkd934/p/9541417.html

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄宝康

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值