Last active
July 30, 2020 14:47
-
-
Save betray32/24931403b0e8b8aff3542419e2e201f1 to your computer and use it in GitHub Desktop.
Logger para llamadas rest con spring
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 cl.tarjetacreditopac.restclient; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import org.apache.commons.logging.Log; | |
import org.apache.commons.logging.LogFactory; | |
import org.springframework.http.HttpRequest; | |
import org.springframework.http.client.ClientHttpRequestExecution; | |
import org.springframework.http.client.ClientHttpRequestInterceptor; | |
import org.springframework.http.client.ClientHttpResponse; | |
/** | |
* LoggingRequestInterceptor - para llamadas rest | |
* | |
* @author ccontrerasc | |
* | |
*/ | |
public class LoggingRequestInterceptor implements ClientHttpRequestInterceptor { | |
/** | |
* LOG | |
*/ | |
private static final Log log = LogFactory.getLog(LoggingRequestInterceptor.class); | |
/** | |
* intercept | |
*/ | |
@Override | |
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) | |
throws IOException { | |
traceRequest(request, body); | |
ClientHttpResponse response = execution.execute(request, body); | |
traceResponse(response); | |
return response; | |
} | |
/** | |
* traceRequest | |
* | |
* @param request | |
* @param body | |
* @throws IOException | |
*/ | |
private void traceRequest(HttpRequest request, byte[] body) throws IOException { | |
log.info("===========================request begin============================================="); | |
log.info("URI = " + request.getURI()); | |
log.info("Method = " + request.getMethod()); | |
log.info("Headers = " + request.getHeaders()); | |
log.info("Request body = " + new String(body, "UTF-8")); | |
log.info("==========================request end================================================"); | |
} | |
/** | |
* traceResponse | |
* | |
* @param response | |
* @throws IOException | |
*/ | |
private void traceResponse(ClientHttpResponse response) throws IOException { | |
StringBuilder inputStringBuilder = new StringBuilder(); | |
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getBody(), "UTF-8")); | |
String 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================================================="); | |
} | |
} |
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
/* | |
* Instanciar el rest para obtener los detalles de la llamada | |
*/ | |
RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory())); | |
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>(); | |
interceptors.add(new LoggingRequestInterceptor()); | |
restTemplate.setInterceptors(interceptors); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment