Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

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.

2. Feign Client

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.

3. Logging Configuration

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

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.

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.

3.1. Java Configuration

We need to declare a config class, let’s call it 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:

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

3.2. Using Properties

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:

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

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

feign.client.config.default.loggerLevel = full

4. Example

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.

Below, we’ll  declare the UserClient class:

@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.

Let’s take a look at how the logging is looking when we call /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": "[email protected]",

    // 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.

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

5. Conclusion

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.

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

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are closed on this article!