How to customize HTTP communication logging with RestTemplate?
Nowadays HTTP communication is very important in enterprise projects. For people who use Spring related technologies an obvious decision is to use RestTemplate for executing HTTP requests. Today, I’m going to describe how to customize HTTP communication logging while using RestTemplate.
Firstly create simple Spring Boot application:
Second step is to configure our application. In the configuration class we define RestTemplate bean. We set interceptor list for our RestTemplate. CustomClientHttpRequestInterceptor is implementing ClientHttpRequestInterceptor interface. It’s allow us to process HTTP requests and responses.
Let’s see how our CustomClientHttpRequestInterceptor looks like. We need to override intercept method which parameters are: a HttpRequest instance, a bytes array with request body and a ClientHttpRequestExecution instance.
We register Spring bean with @Component annotation. In first line we define LOGGER to to write interesting data into standard output or file (depends on configuration). In our method implementation we log request URI on debug level. Then we trace request data. Next step is to execute HttpRequest with given body. Then we can trace response data and log response code on debug level. After all we need to return the response instance. traceRequest and traceResponse methods are quite obvious. If you have any question about them, please leave a comment.
Ok, but we need have some rest HTTP service to exchange data. For this example we will use jsonplaceholder which is free rest api for testing and education purposes.
Let’s define our SimpleHttpClient
As earlier, we register Spring bean, then we can use Spring Dependency Injection to inject dependencies into the bean. The first dependency is the RestTemplate defined in the Application config. Second one is a serviceUrl which is provided in a application.properties file.
Let’s see this file:
We create postForEntity method which is annotated with @PostConstruct annotation. It means that this method will be executed just after bean creation. In our metohd we provide some data to exchange, create a requestUrl and exchange the data using RestTemplate.
Ok, but how to see HTTP communication data which is transfered in this example?
We need to include in out pom a logging starter, with this dependency we can configure our logging.
By default Spring Boot uses Logback as logging framework. In src/main/resources directory we need to create logback-spring.xml file which will be automaticly discovered by Spring Boot.
This file looks like:
Now is the time to explain the meaning of this line:
in the application.properties file. With Spring we can manipulate logging configuration using Spring profiles. Including them in application.properties file is the easiest way to start working with profiles. They can be also passed as a environment variable durring JVM start.
As we can see in the logback configuration file, we have two different configurations for the CustomClientHttpRequestInterceptor LOGGER. For example, when you developing application and you need to see full HTTP communication logs, you can enable TRACE log level with a develop profile. But if your application is tested by users you can enable a uat (user acceptance tests) profile to limit logs only for more important informations (you are free to choice profile names). In a production environment your logs would not be visible if you not specify any profile, because default logging level is INFO.
Let’s start our application with IDE or with maven by typing:
We can see full HTTP communication logs on standard output. Let’s change active spring profiles to:
Now you can see that log output is limited.
In logback-spring.xml you can include external or custom files with logging configuration for specified profile. It’s not limited for single logger. For more details please visit Spring logging reference and Logback site.
You can find the source code in my Github repository how-to under a restemplateexample directory.
Hope you enjoy this post. It shows few Spring features, which you can use in your projects. If You have any questions or problems leave a comment or send email.
See You soon!