我正在尝试为来自 Feign 休息客户端的每个请求进行日志记录。
但是我无法让日志记录正常工作,而“标准”Slf4j 日志记录却可以正常工作。
我有以下内容:
public MyClient() {
initConnectionProperties();
this.service = Feign.builder()
.contract(new JAXRSContract())
.decoder(getJacksonDecoder())
.encoder(getJacksonEncoder())
.requestInterceptor(new BasicAuthRequestInterceptor(user, password))
.logger(new Slf4jLogger(MyClient.class)) //not working
.logLevel(feign.Logger.Level.BASIC)
.target(MyClient.class, this.url);
logger.info("Connection parameters: url = " + url + ", user = " + user); // working
}
您需要在 application.properties 中配置日志记录,如下所示:
logging.level.<package path>.MyClient=DEBUG
如果您使用 application.yml 那么:
logging.level.<package path>.MyClient: DEBUG
可以设置日志级别来告诉Feign要记录多少内容。
选项有:
示例:
logLevel(feign.Logger.Level.NONE)
or
logLevel(feign.Logger.Level.BASIC)
or
logLevel(feign.Logger.Level.HEADERS)
or
logLevel(feign.Logger.Level.FULL)
更多详情可以参考这个
这就是我如何使用自定义配置类进行日志记录
注意 Feign 日志记录仅响应 DEBUG 级别。
配置类
@Configuration
public class UserClientConfig {
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.HEADERS;
}
}
客户
@FeignClient(name = "User", url = "http://localhost:8080",configuration=UserClientConfig.class)
public interface UserClient {
@RequestMapping(method = RequestMethod.GET, value = "/user")
List<User> getAllUsers();
}
应用程序.属性
logging.level.<pcakgepath>.UserClient: DEBUG
首先,您需要将假客户端类的日志记录级别设置为
DEBUG
,正如 Maverick 在他的回答中已经提到的那样。
然后,如果您使用 Spring Boot,除了创建
@Configuration
类的选项(如 Niraj 在他的答案中已经提到的那样)之外,您还可以在应用程序属性/yml 配置文件中单独配置每个客户端:
spring:
cloud:
openfeign:
client:
config:
the_name_of_your_feign_client:
connectTimeout: 5000
readTimeout: 5000
loggerLevel: basic
或者使用默认值而不是
the_name_of_your_feign_client
以相同的方式配置所有假客户端:
spring:
cloud:
openfeign:
client:
config:
default:
connectTimeout: 5000
readTimeout: 5000
loggerLevel: basic
这应该对 Spring Boot 3 有效。 对于旧版本,路径为:
feign:
client:
config:
the_name_of_your_feign_client:
我没有设置客户端,我的客户端调用失败,日志记录不起作用..一旦我添加了 OkHttpClient 并更改了 logback.xml 文件,工作正常
MyApi myApi = Feign.builder()
.client(new OkHttpClient())
.decoder(new JacksonDecoder())
.encoder(new JacksonEncoder())
.logger(new Slf4jLogger())
.logLevel(feign.Logger.Level.FULL)
.target(MyApi.class, "http://localhost:8082");
这是logback.xml
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d %green([%thread]) %highlight(%level) %logger{50} - %msg%n</pattern>
</encoder>
</appender>
<logger name="feign.Logger" level="DEBUG" />
<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
private void setup() {
//...
feignBuilder.logger(new MyLogger());
feignBuilder.logLevel(Logger.Level.FULL);
}
private static class MyLogger extends Logger {
@Override
protected void log(String s, String s1, Object... objects) {
System.out.println(String.format(s + s1, objects)); // Change me!
}
}
您可能还需要将 log4j 日志记录级别配置为
feign
为 DEBUG。如果您使用的是 Spring Boot,对我有用的是:
curl -X POST http://localhost/loggers/feign -H 'Content-Type: application/json' -d '{"configuredLevel": "DEBUG"}'
我有点晚了,但让我试着帮助其他人找到这个帖子。
有两件事我们必须区分。使用普通 Feign 以及与 Spring Cloud OpenFeign 一起使用时的 Feign 日志记录。
使用简单的 Feign,你必须做一些事情才能使事情正常进行:
feign
记录器设置为 DEBUG
让我告诉你如何做:
Feign.builder()
.logLevel(Logger.Level.FULL)
.logger(new Slf4jLogger())
.target(MyClient.class, "http://localhost:8081");
在这种特殊情况下,我使用 SLF4J 作为日志记录适配器。此配置处理了步骤 1 和 2。 现在让我们配置记录器。不要忘记,这可能会根据您的日志记录设置而有所不同,我将向您展示 Spring Boot 的一个:
application.properties
:
logging.level.feign=DEBUG
但这可以通过你的
logback.xml
、log4j 中的普通 Logback 来完成,无论你有什么日志系统。唯一重要的是将 feign
命名记录器配置为 DEBUG
。
这是因为 Feign 库使用
feign
记录器记录所有内容,而不是使用目标客户端的类名。
使用 Spring Cloud OpenFeign,它发生了一些变化,因为 Spring 使用客户端的类名来设置记录器。
对于这种情况,我们只需要做两件事 - 如果您使用
@FeignClient
注释。
DEBUG
第一个可以这样做:
@Configuration
public class FeignConfiguration {
@Bean
public Logger.Level loggerLevel() {
return Logger.Level.FULL;
}
}
这将为所有 Feign 客户端配置日志级别,除非您在
@FeignClient
注释中使用特定配置。
第二件事,为客户端的类/包启用
DEBUG
日志记录。
application.properties
:
logging.level.com.example.MyClient=DEBUG
如果您在 Feign 配置方面遇到困难,您可能想看看我的文章或我的课程。
干杯。
以下代码更改在 kotlin 中对我有用:
import feign.Logger
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
@Configuration
class FeignLoggingConfiguration {
@Bean
fun feignLoggerLevel(): Logger.Level {
return Logger.Level.HEADERS
}
}
在客户端添加该配置类:
@FeignClient(name = "yourClient", url = "\${base-url}", configuration = [FeignLoggingConfiguration::class])
interface yourClient
注意:请确保
yourClient
类的日志记录级别为DEBUG
feign 支持的登录级别:
无,不记录(默认)
BASIC,仅记录请求方法和URL以及响应状态代码和执行时间
HEADERS,记录基本信息以及请求和响应标头
完整,记录请求和响应的标头、正文和元数据
您可以将日志级别放入 bootstrap/application 属性中,它将为您提供 Spring Boot 应用程序的所有调试日志 例如:- logging.leve.root=调试