Feign客户端的日志

一 介绍

很多场景下,需要了解Feign处理请求的具体要求,处理feign远程调用接口的日志打印,那么如何满足这种需求呢?

Feign对日志的处理非常灵活,可为每个Feign客户端指定日志记录策略,每个Feign客户端都会创建一个logger。默认情况下,logger的名称是Feigh接口的完整类名。需要注意的是,Feign的日志打印只会对DEBUG级别做出响应。

我们可以为每个Feign客户端配置各种的Logger.Level对象,告诉Feign记录哪些日志。Logger.Level的值有以下选择。

NONE,无记录(DEFAULT)。

BASIC,只记录请求方法和URL以及响应状态代码和执行时间。

HEADERS,记录基本信息以及请求和响应标头。

FULL,记录请求和响应的头文件,正文和元数据。

二 新建项目myfeign-logging

三 编写Feign的配置类

package com.itmuch.cloud.config;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
import feign.Logger;
 
@Configuration
public class FeignLogConfiguration {
  @Bean
  Logger.Level feignLoggerLevel() {
    return Logger.Level.FULL;
  }
}

四 修改Feign的接口,指定配置类

package com.itmuch.cloud.study.user.feign;
 
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
import com.itmuch.cloud.config.FeignLogConfiguration;
import com.itmuch.cloud.study.user.entity.User;
 
@FeignClient(name = "microservice-provider-user", configuration = FeignLogConfiguration.class)
public interface UserFeignClient {
  @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  public User findById(@PathVariable("id") Long id);
}

五 修改application.yml

server:
  port: 8010
spring:
  application:
    name: microservice-consumer-movie
eureka:
  client:
    serviceUrl:
      defaultZone:http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
logging:
  level:
    com.itmuch.cloud.study.user.feign.UserFeignClient: DEBUG # 将Feign接口的日志级别设置成DEBUG,因为Feign的Logger.Level只对DEBUG作出响应。

    #logging.level.org.springframework.cloud.consul=DEBUG
    #将feign接口的日志设置为DEBUG,针对于consul使用,这里只是对UserFeignClient操作,其实是可以这样做的,记录一下

六 测试

1 启动consul

2 启动user微服务

3 启动feign

4 访问http://localhost:8010/user/1

2018-06-17 14:31:40.350 DEBUG 3876 --- [provider-user-2] c.i.c.study.user.feign.UserFeignClient   : [UserFeignClient#findById] ---> GET http://microservice-provider-user/1 HTTP/1.1

2018-06-17 14:31:40.373 DEBUG 3876 --- [provider-user-2] c.i.c.study.user.feign.UserFeignClient   : [UserFeignClient#findById] <--- HTTP/1.1 200 (23ms)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值