RequestLog is an HTTP request logging and retry compensation tool based on Spring Boot.
Features: Low intrusion and easy integration.
Taking RestTemplate as an example.
<dependency>
<groupId>io.github.requestlog</groupId>
<artifactId>request-log-resttemplate-starter</artifactId>
<version>${latest_stable_version}</version>
</dependency>
Enhance the RestTemplate client using annotations.
@RequestLogEnhanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Component
public class MyRequestLogRepository implements IRequestLogRepository {
@Override
public void saveRequestLog(RequestLog requestLog) {
// save request log
}
@Override
public void saveRequestLogAndRetryJob(RequestLog requestLog, RequestRetryJob requestRetryJob) {
// save request log and retry job
}
}
// Original request code
String result = restTemplate.getForObject("url", String.class);
// Wrap the request using LogContext
String wrappedResult = LogContext.log().execute(() -> {
return restTemplate.getForObject("url", String.class);
});
For the wrapped request, when an exception occurs or the response status code is not 2xx
, the corresponding save method of the custom IRequestLogRepository
will be invoked.