-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
92ddb34
commit d0fe553
Showing
4 changed files
with
114 additions
and
73 deletions.
There are no files selected for viewing
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
105 changes: 105 additions & 0 deletions
105
...ain/java/com/example/restclient/bootrestclient/config/ClientLoggerRequestInterceptor.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package com.example.restclient.bootrestclient.config; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpRequest; | ||
import org.springframework.http.HttpStatusCode; | ||
import org.springframework.http.client.ClientHttpRequestExecution; | ||
import org.springframework.http.client.ClientHttpRequestInterceptor; | ||
import org.springframework.http.client.ClientHttpResponse; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class ClientLoggerRequestInterceptor implements ClientHttpRequestInterceptor { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(ClientLoggerRequestInterceptor.class); | ||
|
||
@Override | ||
public ClientHttpResponse intercept( | ||
HttpRequest request, byte[] body, ClientHttpRequestExecution execution) | ||
throws IOException { | ||
logRequest(request, body); | ||
ClientHttpResponse response = execution.execute(request, body); | ||
return logResponse(response); | ||
} | ||
|
||
private BufferingClientHttpResponseWrapper logResponse(ClientHttpResponse response) | ||
throws IOException { | ||
log.info( | ||
"============================response begin=========================================="); | ||
log.info("Status code : {}", response.getStatusCode()); | ||
log.info("Status text : {}", response.getStatusText()); | ||
logHeaders(response.getHeaders()); | ||
byte[] responseBody = response.getBody().readAllBytes(); | ||
if (responseBody.length > 0) { | ||
log.info("Response body: {}", new String(responseBody, StandardCharsets.UTF_8)); | ||
} | ||
log.info( | ||
"=======================response end================================================="); | ||
return new BufferingClientHttpResponseWrapper(response, responseBody); | ||
} | ||
|
||
private void logRequest(HttpRequest request, byte[] body) { | ||
|
||
log.info( | ||
"===========================request begin================================================"); | ||
log.info("URI : {}", request.getURI()); | ||
log.info("Method : {}", request.getMethod()); | ||
logHeaders(request.getHeaders()); | ||
if (body.length > 0) { | ||
log.info("Request body: {}", new String(body, StandardCharsets.UTF_8)); | ||
} | ||
log.info( | ||
"==========================request end================================================"); | ||
} | ||
|
||
private void logHeaders(HttpHeaders headers) { | ||
log.info("Headers : "); | ||
headers.forEach((name, values) -> values.forEach(value -> log.info("{}={}", name, value))); | ||
} | ||
|
||
private static class BufferingClientHttpResponseWrapper implements ClientHttpResponse { | ||
private final ClientHttpResponse response; | ||
private final byte[] body; | ||
|
||
public BufferingClientHttpResponseWrapper(ClientHttpResponse response, byte[] body) { | ||
this.response = response; | ||
this.body = body; | ||
} | ||
|
||
@Override | ||
public InputStream getBody() throws IOException { | ||
return new ByteArrayInputStream(body); | ||
} | ||
|
||
@Override | ||
public HttpHeaders getHeaders() { | ||
return response.getHeaders(); | ||
} | ||
|
||
@Override | ||
public HttpStatusCode getStatusCode() throws IOException { | ||
return response.getStatusCode(); | ||
} | ||
|
||
@Override | ||
public int getRawStatusCode() throws IOException { | ||
return response.getRawStatusCode(); | ||
} | ||
|
||
@Override | ||
public String getStatusText() throws IOException { | ||
return response.getStatusText(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
response.close(); | ||
} | ||
} | ||
} |
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
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