在使用 feign 发送请求的测试过程中 无法看到发送的url以及一些请求头,需要配置日志信息
一、给指定的FeignClient添加日志信息
1. 使用配置类的方式
FeignConfig
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//@Configuration //当使用了@Configuration注解之后,该配置作用于所有的FeignClient,如果只是想要局部配置,就不要加该注解
public class FeignConfig {
@Bean
public Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
FeignClient
import com.example.mytest.config.FeignConfig;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.Map;
@FeignClient(name = "payment", url = "${payment:}", configuration = FeignConfig.class)
public interface PaymentFeignClient {
@RequestMapping(value = "/hera_profactory/v1/getExportProdConfig", method = RequestMethod.POST)
String getProductConfig(Map<String, String> params);
}
bootstrap.yml
# SpringBoot的默认日志级别是info级别,feign的日志级别是debug级别,info > debug,所以在默认情况下,feign的日志不会输出
logging:
level:
# 指定包或类,这里指定了具体哪个FeignClient
com.example.mytest.feignClients.PaymentFeignClient: debug
2. 使用配置文件的方式
bootstrap.yml
# SpringBoot的默认日志级别是info级别,feign的日志级别是debug级别,info > debug,所以在默认情况下,feign的日志不会输出
logging:
level:
# 指定包或类,这里指定了具体哪个FeignClient;feign接口日志只对debug有反应
com.example.mytest.feignClients.PaymentFeignClient: debug
feign:
client:
config:
# @FeignClient 的 name 或 value 值
payment:
loggerLevel: full
二、给所有的FeignClient添加日志信息
1. 使用配置类
FeignConfig
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeignConfig {
@Bean
public Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
启动类
import com.example.mytest.config.FeignConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
//@EnableFeignClients(defaultConfiguration = FeignConfig.class) //2023-02-15更新:配置类使用@Configuration后就是全局配置
@EnableFeignClients
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
bootstrap.yml
# SpringBoot的默认日志级别是info级别,feign的日志级别是debug级别,info > debug,所以在默认情况下,feign的日志不会输出
logging:
level:
# 指定包或类,这里指定存放所有FeignClient的包
com.example.mytest.feignClients: debug
2. 使用配置文件
bootstrap.yml
# SpringBoot的默认日志级别是info级别,feign的日志级别是debug级别,info > debug,所以在默认情况下,feign的日志不会输出
logging:
level:
# 指定包或类,这里指定存放所有FeignClient的包
com.example.mytest.feignClients: debug
feign:
client:
config:
# feign 全局设置
default:
loggerLevel: full
三、全局和指定配置可以配合使用 如:只指定某一个 FeignClient 的日志级别为 FULL,其他的为 BASIC
bootstrap.yml
logging:
level:
# 指定包或类,这里指定存放所有FeignClient的包;feign接口日志只对debug有反应
com.example.mytest.feignClients: debug
feign:
client:
config:
# feign 全局日志设置
default:
loggerLevel: basic
# @FeignClient 的 name 或 value 值
payment:
loggerLevel: full
配置文件的方式可以这样,配置类还没试过,应该也行~
四、日志级别说明
NONE
【性能最佳,适用于生产】:不记录任何日志(默认值)
BASIC
【适用于生产环境追踪问题】:仅记录请求方法、URL、响应状态代码以及执行时间
HEADERS
: 在 BASIC
的基础上打印请求和响应的头
FULL
:【比较适用于开发以及测试环境定位问题】:记录请求和响应的header、body和元数据