Last active
May 28, 2020 07:30
-
-
Save MatthiasKainer/5cb8f10667b0122795b933bd5788943c to your computer and use it in GitHub Desktop.
Logging everything from a Spring RestTemplate with Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.matthiaskainer.spring.net.resttemplate.logging | |
import mu.KotlinLogging | |
import org.springframework.http.HttpRequest | |
import org.springframework.http.client.ClientHttpRequestExecution | |
import org.springframework.http.client.ClientHttpRequestInterceptor | |
import org.springframework.http.client.ClientHttpResponse | |
import java.io.BufferedReader | |
import java.io.IOException | |
import java.io.InputStreamReader | |
import java.nio.charset.Charset | |
private val log = KotlinLogging.logger {} | |
class LoggingRequestInterceptor : ClientHttpRequestInterceptor { | |
@Throws(IOException::class) | |
override fun intercept(request: HttpRequest, body: ByteArray, execution: ClientHttpRequestExecution): ClientHttpResponse { | |
traceRequest(request, body) | |
val response: ClientHttpResponse = execution.execute(request, body) | |
traceResponse(response) | |
return response | |
} | |
@Throws(IOException::class) | |
private fun traceRequest(request: HttpRequest, body: ByteArray) { | |
log.info("===========================request begin================================================") | |
log.info("URI : {}", request.getURI()) | |
log.info("Method : {}", request.getMethod()) | |
log.info("Headers : {}", request.getHeaders()) | |
log.info("Request body: {}", String(body, Charset.forName("UTF-8"))) | |
log.info("==========================request end================================================") | |
} | |
@Throws(IOException::class) | |
private fun traceResponse(response: ClientHttpResponse) { | |
val inputStringBuilder = StringBuilder() | |
val bufferedReader = BufferedReader(InputStreamReader(response.getBody(), "UTF-8")) | |
var line = bufferedReader.readLine() | |
while (line != null) { | |
inputStringBuilder.append(line) | |
inputStringBuilder.append('\n') | |
line = bufferedReader.readLine() | |
} | |
log.info("============================response begin==========================================") | |
log.info("Status code : {}", response.getStatusCode()) | |
log.info("Status text : {}", response.getStatusText()) | |
log.info("Headers : {}", response.getHeaders()) | |
log.info("Response body: {}", inputStringBuilder.toString()) | |
log.info("=======================response end=================================================") | |
} | |
} | |
// Usage requires a BufferingClientHttpRequestFactory | |
private fun requestFactory() = BufferingClientHttpRequestFactory(YourRequestFactory()) | |
RestTemplateBuilder() | |
.requestFactory { requestFactory() } | |
.interceptors(LoggingRequestInterceptor()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment