【微服务|openfeign】Feign的日志记录

Feign为每个Feign 客户端创建一个logger。默认情况下,logger的名称是创建 Feign 客户端的接口的完整类名。

简单来说,Feign提供了日志打印功能,我们可以通过配置来调整日志级别,从而了解Feign中Http请求的细节,也就是对Feign接口的调用情况进行监控和输出。

因为feign调试日志是debug级别输出,springboot默认的日志级别是info,所以feign的debug日志级别就不会输出。

logging.level=debug这样配置是对所有的日志级别进行配置。

该场景只需要对feign接口进行debug配置,所以是这样配置logging.level.com.ossa.feign.api=debug

在yaml中为如下配置

logging:
  level:
    com.ossa.feign.api: debug

com.ossa.feign.api下存放所有的FeignClient接口

全局日志处理

这里注意,我们这样做是对所有的feign接口做统一日志处理,如果相对个别服务的日志做统一处理,可以通过配置文件处理,如下:

配置文件

feign:
  sentinel:
    enabled: true
  client:
    config:
      # 提供方的服务名
      ossa-service-producer:
        #请求日志级别
        loggerLevel: NONE

配置类

import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author issavior
 */

@Configuration
public class FeignClientConfig {

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

独立服务日志处理

配置文件方式

在消费者服务的配置文件做如下处理

feign:
  client:
    config:
      # 提供方的服务名
      ossa-service-producer:
        #请求日志级别
        loggerLevel: NONE

配置类方式

  1. 自定义一个配置类

    import feign.Logger;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;

    /**
     * @author issavior
     */

    @Configuration
    public class FeignClientConfig {

        @Bean
        public Logger.Level feignLoggerLevel(){
            return Logger.Level.FULL;
        }
    }
  2. 在@FeignClient注解处指定该配置文件,覆盖掉默认的配置文件

    import com.ossa.feign.config.FeignClientConfig;
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;

    /**
     * @author issavior
     */

    @FeignClient(value = "ossa-service-producer", configuration = FeignClientConfig.class)
    @RequestMapping(value 
    "/producer")
    public interface ProducerFeign {

        /**
         * 根据ID查询商品
         *
         * @param id 商品的主键ID
         * @return 相关商品的信息
         */

        @GetMapping(value = "/{id}")
        String producerById(@PathVariable("id") String id);

    }


原文始发于微信公众号(步尔斯特):【微服务|openfeign】Feign的日志记录

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/48085.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!