Feign的日志

博客介绍了Feign处理请求的日志要求,其日志处理灵活,可指定日志记录策略,有NONE、BASIC等多种日志级别可选。还详细说明了新建项目、编写配置类、修改接口和配置文件等步骤,并给出测试流程,包括启动相关服务和访问特定URL。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 介绍
很多场景下,需要了解Feign处理请求的具体要求,那么如何满足这种需求呢?
Feign对日志的处理非常灵活,可为每个Feign客户端指定日志记录策略,每个Feign客户端都会创建一个logger。默认情况下,logger的名称是Feigh接口的完整类名。需要注意的是,Feign的日志打印只会对DEBUG级别做出响应。
我们可以为每个Feign客户端配置各种的Logger.Level对象,告诉Feign记录哪些日志。Logger.Level的值有以下选择。
NONE,无记录(DEFAULT)。
BASIC,只记录请求方法和URL以及响应状态代码和执行时间。
HEADERS,记录基本信息以及请求和响应标头。
FULL,记录请求和响应的头文件,正文和元数据。
二 新建项目microservice-consumer-movie-feign-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.BASIC;
  }
}
四 修改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作出响应。
六 测试
1 启动eureka
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)

 

转载于:https://my.oschina.net/xiaominmin/blog/3052946

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值