Feign Logging Configuration – 伪装日志配置

最后修改: 2021年 6月 7日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’ll describe how we can enable Feign client to log in to our Spring Boot application. Also, we’ll take a look at different types of configurations for it. For a refresher on Feign client, check out our comprehensive guide.

在本教程中,我们将介绍如何启用Feign客户端来登录我们的Spring Boot应用程序。此外,我们还将看一下它的不同类型的配置。如需了解有关Feign客户端的复习情况,请查看我们的综合指南

2. Feign Client

2. 佯装客户

Feign is a declarative web service client that works by processing annotations into a templatized request. Using a Feign client, we get rid of boilerplate code to make the HTTP API requests. We just need to put in an annotated interface. Thus, the actual implementation will be created at runtime.

Feign是一个声明式的Web服务客户端,它通过将注释处理为模板化的请求来工作。使用Feign客户端,我们摆脱了进行HTTP API请求的模板代码。我们只需要放入一个注解的接口。因此,实际的实现将在运行时被创建。

3. Logging Configuration

3.日志配置

Feign client logging helps us to have a better view of the requests that have been made. To enable logging, we need to set the Spring Boot logging level to DEBUG for the class or package that contains our feign client in the application.properties file

要启用日志,我们需要在application.properties文件中,将包含我们的feign客户端的的Spring Boot日志级别设置为DEBUG。

Let’s set the logging level property for a class:

让我们为一个类设置日志级别属性。

logging.level.<packageName>.<className> = DEBUG

Or if we have a package where we put all our feign clients, we can add it for the whole package:

或者,如果我们有一个包,我们把所有的佯装客户,我们可以为整个包添加它。

logging.level.<packageName> = DEBUG

Next, we need to set the logging level for the feign client. Notice that the previous step was just to enable the logging.

接下来,我们需要为feign客户端设置日志记录级别。注意,上一步只是启用了日志记录。

There are four logging levels to choose from:

有四个记录级别可供选择。

  • NONE: no logging (DEFAULT)
  • BASIC: logs the request method and URL and the response status code and execution time
  • HEADERS: logs the basic information along with the request and response headers
  • FULL: logs the headers, body, and metadata for both requests and responses

We can configure them via java configuration or in our properties file.

我们可以通过java配置或在我们的属性文件中配置它们。

3.1. Java Configuration

3.1.Java配置

We need to declare a config class, let’s call it FeignConfig:

我们需要声明一个配置类,就叫它FeignConfig吧。

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

After that, we’ll bind the configuration class into our feign client class FooClient:

之后,我们将把配置类绑定到我们的feign客户类FooClient

@FeignClient(name = "foo-client", configuration = FeignConfig.class)
public interface FooClient {
 
    // methods for different requests
}

3.2. Using Properties

3.2.使用属性

The second way is setting it in our application.properties. Let’s reference here the name of our feign client, in our case foo-client:

第二种方法是在我们的 application.properties.中设置。让我们在这里引用我们的feign client的名字,在我们的例子中是foo-client

feign.client.config.foo-client.loggerLevel = full

Or, we can override the default config level for all feign clients:

或者,我们可以覆盖所有feign客户端的默认配置级别。

feign.client.config.default.loggerLevel = full

4. Example

4.例子

For this example, we have configured a client to read from the JSONPlaceHolder APIs. We’ll retrieve all the users with the help of the feign client.

在这个例子中,我们已经配置了一个客户端,以便从JSONPlaceHolder APIs中读取。我们将在feign客户端的帮助下检索到所有的用户。

Below, we’ll  declare the UserClient class:

下面,我们将声明UserClient类。

@FeignClient(name = "user-client", url="https://jsonplaceholder.typicode.com", configuration = FeignConfig.class)
public interface UserClient {

    @RequestMapping(value = "/users", method = RequestMethod.GET)
    String getUsers();
}

We’ll be using the same FeignConfig we created in the Configuration section.  Notice that the logging level continues to be Logger.Level.FULL.

我们将使用我们在配置部分创建的同一个FeignConfig。 注意,日志级别仍然是Logger.Level.FULL

Let’s take a look at how the logging is looking when we call /users:

让我们看看当我们调用/users时,日志记录的情况如何。

2021-05-31 17:21:54 DEBUG 2992 - [thread-1] com.baeldung.UserClient : [UserClient#getUsers] ---> GET https://jsonplaceholder.typicode.com/users HTTP/1.1
2021-05-31 17:21:54 DEBUG 2992 - [thread-1] com.baeldung.UserClient : [UserClient#getUsers] ---> END HTTP (0-byte body)
2021-05-31 17:21:55 DEBUG 2992 - [thread-1] com.baeldung.UserClient : [UserClient#getUsers] <--- HTTP/1.1 200 OK (902ms)
2021-05-31 17:21:55 DEBUG 2992 - [thread-1] com.baeldung.UserClient : [UserClient#getUsers] access-control-allow-credentials: true
2021-05-31 17:21:55 DEBUG 2992 - [thread-1] com.baeldung.UserClient : [UserClient#getUsers] cache-control: max-age=43200
2021-05-31 17:21:55 DEBUG 2992 - [thread-1] com.baeldung.UserClient : [UserClient#getUsers] content-type: application/json; charset=utf-8
2021-05-31 17:21:55 DEBUG 2992 - [thread-1] com.baeldung.UserClient : [UserClient#getUsers] date: Mon, 31 May 2021 14:21:54 GMT
                                                                                            // more headers
2021-05-31 17:21:55 DEBUG 2992 - [thread-1] com.baeldung.UserClient : [UserClient#getUsers] [
  {
    "id": 1,
    "name": "Leanne Graham",
    "username": "Bret",
    "email": "Sincere@april.biz",

    // more user details
  },

  // more users objects
]
2021-05-31 17:21:55 DEBUG 2992 - [thread-1] com.baeldung.UserClient : [UserClient#getPosts] <--- END HTTP (5645-byte body)

In the first part of the log, we can see the request logged; The URL endpoint with his HTTP GET method. In this case, as it is a GET request, we don’t have a request body.

在日志的第一部分,我们可以看到记录的request;URL端点与他的HTTP GET方法。在这种情况下,由于它是一个GET request,我们没有一个请求体。

The second part contains the response. It shows the headers and the body of the response.

第二部分包含响应。它显示了响应的标头正文

5. Conclusion

5.总结

In this short tutorial, we have looked at the Feign client logging mechanism and how we can enable it. We saw that there are multiple ways to configure it based on our needs.

在这个简短的教程中,我们看了Feign客户端日志机制以及如何启用它。我们看到,有多种方法可以根据我们的需要来配置它。

As always, the example shown in this tutorial is available over on GitHub.

一如既往,本教程中显示的示例可在GitHub上获取。