How to add DEBUG log to spring RestClient
Add the dependency to Apache HTTP client:
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.3.1</version>
</dependency>
Do not use the version 5.4.0. It's producing some missing class/method...
Then when creating your RestClient
, use the HttpComponentsClientHttpRequestFactory
:
var restTemplateBuilder = new RestTemplateBuilder()
.setConnectTimeout(Duration.ofSeconds(1))
.setReadTimeout(Duration.ofSeconds(1))
.additionalInterceptors(restHttpRetryInterceptor);
var restClientBuilder = RestClient.builder(restTemplateBuilder.build())
.requestFactory(new HttpComponentsClientHttpRequestFactory());
And finally, configure your log levels, for example in your application.yml
:
logging:
level:
org.apache.httpcomponents: DEBUG
org.apache.hc.client5: DEBUG
Your should then have your outgoing HTTP requests loggued:
2024-10-22 14:04:25.870 [080-exec-2] DEBUG org.apache.hc.client5.http.headers [] http-outgoing-0 >> POST /foobar HTTP/1.1
2024-10-22 14:04:25.870 [080-exec-2] DEBUG org.apache.hc.client5.http.headers [] http-outgoing-0 >> Accept: application/json
2024-10-22 14:04:25.870 [080-exec-2] DEBUG org.apache.hc.client5.http.headers [] http-outgoing-0 >> Content-Type: application/json
2024-10-22 14:04:25.871 [080-exec-2] DEBUG org.apache.hc.client5.http.headers [] http-outgoing-0 >> Authorization: Bearer jwt
2024-10-22 14:04:25.871 [080-exec-2] DEBUG org.apache.hc.client5.http.headers [] http-outgoing-0 >> Accept-Encoding: gzip, x-gzip, deflate
2024-10-22 14:04:25.871 [080-exec-2] DEBUG org.apache.hc.client5.http.headers [] http-outgoing-0 >> Content-Length: 50
2024-10-22 14:04:25.871 [080-exec-2] DEBUG org.apache.hc.client5.http.headers [] http-outgoing-0 >> Host: localhost:9090
2024-10-22 14:04:25.871 [080-exec-2] DEBUG org.apache.hc.client5.http.headers [] http-outgoing-0 >> Connection: keep-alive
2024-10-22 14:04:25.871 [080-exec-2] DEBUG org.apache.hc.client5.http.headers [] http-outgoing-0 >> User-Agent: Apache-HttpClient/5.3.1 (Java/21.0.4)
...