All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.arextest.schedule.client.LoggingRequestInterceptor Maven / Gradle / Ivy

There is a newer version: 2.0.4
Show newest version
package com.arextest.schedule.client;

import java.io.IOException;
import java.util.Base64;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;

/**
 * supply a interceptor to log request info
 * 

* created by xinyuan_wang on 2023/7/26 */ @Slf4j @Component @Order(2) public class LoggingRequestInterceptor implements ClientHttpRequestInterceptor { @Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { ClientHttpResponse response; try { response = execution.execute(request, body); } catch (IOException ex) { LOGGER.warn("Failed to send {} request to {}, body: {}, {}", request.getMethod(), request.getURI(), getRequestBody(body), ex.toString()); throw ex; } HttpStatus statusCode = response.getStatusCode(); // Log the failed request if the response status code is 4xx or 5xx if (statusCode.is4xxClientError() || statusCode.is5xxServerError()) { LOGGER.warn("Failed to send {} request to {}, requestBody: {}, statusCode: {}", request.getMethod(), request.getURI(), getRequestBody(body), statusCode); } return response; } private String getRequestBody(byte[] body) { String requestBody = null; if (body != null && body.length > 0) { requestBody = Base64.getEncoder().encodeToString(body); } return requestBody; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy