OpenFeign的日志增强

OpenFeign虽然提供了日志增强功能,但是默认是不显示任何日志的,不过开发者在调试阶段可以自己配置日志的级别。

openFeign的日志级别如下:

  • NONE:默认的,不显示任何日志
  • BASIC:仅记录请求方法、URL、响应状态码及执行时间
  • HEADERS:除了BASIC中定义的信息之外,还有请求和响应的头信息
  • FULL:除了HEADERS中定义的信息之外,还有请求和响应的正文及元数据

提前说明:当前Spring Cloud OpenFeign的版本为2.1.1 

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>2.1.1.RELEASE</version>
</dependency>

第一种:以配置文件方式设置Feign的日志记录级别

import feign.Response;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;

@Component
@FeignClient(name = "user", url = "http://127.0.0.1:8080")
public interface UserClient {

    @PostMapping(value = "/test/getUserInfo",consumes = MediaType.APPLICATION_JSON_VALUE)
    String getUser(Model model);
}

 在配置文件yml中配置Feign的日志记录级别,特别提醒:在为某一个FeignClient文件设置日志记录级别的时候,一定要先设置好日志级别,推荐日志级别设置为DEBUG

logging:
  level:
    # 指定某一个文件的日志级别
    com.bc.feign.UserFeignClient: DEBUG
    # 为所写服务应用的最顶层的包目录设置日志级别
    #com.bc: info

feign:
  client:
    config:
      # 要调用服务的名称
      user:
        # 设置日志记录级别,其取值共有none、basic、headers、full
        loggerLevel: none

 如上所示就已经为UserClient设置Feign的日志记录级别

feign:
  client:
    config:
      # feign 全局日志记录级别配置,此处将“要调用服务的名称”更改为“default”即可
      default:
        loggerLevel: none

第二种:以Java代码方式设置Feign的日志记录级别

首先需要自定义一个配置类,在其中设置日志级别,如下所示:

package com.example.demo.config;

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

public class OpenFeignConfiguration {

    @Bean
    Logger.Level feignLoggerLevel(){
        // 这里记录所有,根据实际情况选择合适的日志level,此处取值共有四个NONE、BASIC、HEADERS和FULL
        return Logger.Level.FULL;
    }
}

注意:这里的logger是feign包里的

然后在所在的Feign Client中引入上述OpenFeignConfiguration.class配置类,如下所示:

package com.example.demo.feign;

import com.example.demo.config.OpenFeignConfiguration;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value = "openFeign-provider", url = "http://127.0.0.1:8001/", configuration = OpenFeignConfiguration.class)
public interface OpenFeignService {

    @GetMapping("/order/create")
    String create(@RequestParam("id") Integer id);
}

 最后需要在yml配置文件中调整指定包或者openFeign的接口日志级别,如下所示:

#日志配置
logging:
  level:
    # 指定某一个文件的日志级别
    com.example.demo.feign.OpenFeignService: DEBUG
    # 为所写服务应用的最顶层的包目录设置日志级别
    #com.example.demo: info

这里的com.example.demo.feign.OpenFeignService配置的是OpenFeignService接口的日志打印级别(只有这一个Feign调用生效),当然我们也可以配置为一个特定的包。 

特别提示:在FeignConfiguration 配置类上面方法上不需要@Configuration注解,否则会被所有的FeignClient共享,如果添加了注解,则需要将此类放到启动时扫描不到的包

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

//如果是想仅针对一个feign添加日志记录级别,则在此类上方不要添加 @Configuration 注解类
@Configuration
public class OpenFeignConfiguration {

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

第三种:在启动类(SpringBootApplication)中配置Feign 全局日志记录级别(了解即可)

 定义 FeignConfiguration 类,此类也不需要@Configuration注解

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

//@Configuration
public class FeignConfiguration {

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

 启动类的@EnableFeignClients注解上配置 FeignConfiguration 

@EnableFeignClients(defaultConfiguration = FeignConfiguration.class)
import com.ybc.config.FeignConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableFeignClients(defaultConfiguration = FeignConfiguration.class)
@EnableDiscoveryClient
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

  yml配置文件中配置日志级别

#日志配置
logging:
  level:
    # 指定某一个文件的日志级别
    com.bc.feign.UserFeignClient: DEBUG
    # 为所写服务应用的最顶层的包目录设置日志级别
    #com.bc: info
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值