This article shows debug logging of RestTemplate/RestOperations.
🐠 Set DEBUG to log level for RestTemplate Set DEBUG to log level for RestTemplate with src/main/resources/application.yml
.
logging: level: org.springframework.web.client.RestTemplate: DEBUG
🎳 Add logging process to Request/Response This is a sample code for adding logging process to Request/Response by ClientHttpRequestInterceptor
.
Implement ClientHttpRequestInterceptor Interface This is a sample code of implemented ClientHttpRequestInterceptor
interface. Please create app/util/RestTemplateLoggingInterceptor.kt
and add the following code.
import org.slf4j.LoggerFactoryimport org.springframework.http.HttpHeadersimport org.springframework.http.HttpRequestimport org.springframework.http.HttpStatusimport org.springframework.http.client.*import org.springframework.lang.Nullableimport org.springframework.util.StreamUtilsimport java.io.*import java.io.ByteArrayInputStreamimport java.io.IOExceptionclass RestTemplateLoggingInterceptor : ClientHttpRequestInterceptor { private val log = LoggerFactory.getLogger(HelpDeskController::class .java ) @Throws(IOException::class) override fun intercept (request: HttpRequest , body: ByteArray , execution: ClientHttpRequestExecution ) : ClientHttpResponse { log.info("RestTemplate Request: URI={}, Headers={}, Body={}" , request.uri, request.headers, String(body)) val response = BufferingClientHttpResponseWrapper(execution.execute(request, body)) val inputStringBuilder = StringBuilder() val bufferedReader = BufferedReader(InputStreamReader(response.body, "UTF-8" )) var line: String? = bufferedReader.readLine() while (line != null ) { inputStringBuilder.append(line) inputStringBuilder.append('\n' ) line = bufferedReader.readLine() } log.info("RestTemplate Response: StatusCode={} {}, Headers={}, Body={}" , response.statusCode, response.statusText, response.headers, inputStringBuilder.toString() ) return response } private class BufferingClientHttpResponseWrapper internal constructor (private val response: ClientHttpResponse) : ClientHttpResponse { override fun getHeaders () : HttpHeaders { return this .response.headers } override fun getStatusCode () : HttpStatus { return this .response.statusCode } override fun getRawStatusCode () : Int { return this .response.rawStatusCode } override fun close () { this .response.close() } override fun getStatusText () : String { return this .response.statusText } override fun getBody () : InputStream { if (this .body == null ) { this .body = StreamUtils.copyToByteArray(this .response.body) } return ByteArrayInputStream(this .body) } @Nullable private var body: ByteArray? = null } }
Load the above Interceptor Load the Interceptor created earlier.
import app.util.RestTemplateLoggingInterceptorimport org.springframework.context.annotation .Configurationimport org.springframework.boot.web.client.RestTemplateBuilderimport org.springframework.context.annotation .Beanimport org.springframework.web.client.RestOperations@Configuration class RestOperationsConfig { @Bean fun restOperations () : RestOperations { return RestTemplateBuilder().additionalInterceptors(RestTemplateLoggingInterceptor()).build() } }
Logging sample After then you can see request/response information in your log.
level:INFO RestTemplate Request: URI=https://example.com, Headers={Accept=[text/plain, application/json, application/*+json, */*], Content-Length=[0]}, Body= level:INFO RestTemplate Response: StatusCode=200 OK, Headers={Accept-Ranges=[bytes], Cache-Control=[max-age=604800], Content-Type=[text/html], Date=[Wed, 09 May 2018 08:23:02 GMT], Expires=[Wed, 16 May 2018 08:23:02 GMT], Last-Modified=[Fri, 09 Aug 2013 23:54:35 GMT], Body=\n\n\n
🍮 References
🖥 Recommended VPS Service
VULTR provides high performance cloud compute environment for you.
Vultr has 15 data-centers strategically placed around the globe, you can use a VPS with 512 MB memory for just $ 2.5 / month ($ 0.004 / hour).
In addition, Vultr is up to 4 times faster than the competition, so please check it => Check Benchmark Results !!