Spring Boot 中整合 Feign 客户端时,配置日志的多种方式
1. 配置日志级别
可以通过在application.properties或application.yml文件中设置 Feign 客户端接口的日志级别来控制日志输出。
application.properties 配置示例
# 设置Feign客户端日志级别,这里的com.example.client是Feign客户端接口所在的包名
logging.level.com.example.client=DEBUG
application.yml 配置示例
logging:
level:
com.example.client: DEBUG
2. 配置 Feign 日志记录器级别
在代码中配置 Feign 日志记录器的级别,这可以更细粒度地控制日志输出。
步骤
- 创建配置类:创建一个配置类来指定 Feign 日志记录器的级别。
logging:
level:
com.example.client: DEBUG
在上述代码中,Logger.Level 有以下几种可选值:
- NONE:不记录任何日志。
- BASIC:仅记录请求方法、URL、响应状态码和执行时间。
- HEADERS:记录基本信息以及请求和响应的头信息。
- FULL:记录请求和响应的头信息、正文和元数据。
- 在 Feign 客户端接口上使用配置类:
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(name = "example-service", configuration = FeignConfig.class)
public interface ExampleClient {
@GetMapping("/example")
String getExample();
}
3. 全局配置 Feign 日志
如果你想对所有的 Feign 客户端应用相同的日志配置,可以创建一个全局的配置类。
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class GlobalFeignConfig {
@Bean
public Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
然后在application.properties或application.yml中指定全局配置类:
# application.properties
feign.client.default-to-properties=false
feign.client.config.default.loggerLevel=FULL
# application.yml
feign:
client:
default-to-properties: false
config:
default:
loggerLevel: FULL