diff --git a/pom.xml b/pom.xml
index f83628c..ee4d2ab 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
com.treblle
treblle-spring-boot-starter
- 2.0.4
+ 2.0.5
treblle-spring-boot-starter
Official Treblle Starter for Spring Boot
https://github.com/Treblle/treblle-spring
diff --git a/src/main/java/com/treblle/spring/configuration/TreblleProperties.java b/src/main/java/com/treblle/spring/configuration/TreblleProperties.java
index 1faa28f..ee4030f 100644
--- a/src/main/java/com/treblle/spring/configuration/TreblleProperties.java
+++ b/src/main/java/com/treblle/spring/configuration/TreblleProperties.java
@@ -12,6 +12,7 @@ public class TreblleProperties {
private String apiKey;
private String projectId;
private Integer filterOrder = Ordered.LOWEST_PRECEDENCE - 10; // Similar to HttpTraceFilter
+ private boolean debug = false;
private List urlPatterns = Collections.emptyList();
private List maskingKeywords = Collections.emptyList();
@@ -47,6 +48,14 @@ public void setFilterOrder(Integer filterOrder) {
this.filterOrder = filterOrder;
}
+ public boolean isDebug() {
+ return debug;
+ }
+
+ public void setDebug(boolean debug) {
+ this.debug = debug;
+ }
+
public List getUrlPatterns() {
return urlPatterns;
}
diff --git a/src/main/java/com/treblle/spring/service/TreblleServiceImpl.java b/src/main/java/com/treblle/spring/service/TreblleServiceImpl.java
index 5f6e0cb..157c29e 100644
--- a/src/main/java/com/treblle/spring/service/TreblleServiceImpl.java
+++ b/src/main/java/com/treblle/spring/service/TreblleServiceImpl.java
@@ -14,14 +14,19 @@
import org.springframework.core.env.Environment;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpRequest;
import org.springframework.http.MediaType;
+import org.springframework.http.client.*;
import org.springframework.scheduling.annotation.Async;
+import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.security.SecureRandom;
import java.time.Duration;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
@@ -33,7 +38,7 @@
public class TreblleServiceImpl implements TreblleService {
- private static final Logger log = LoggerFactory.getLogger(TreblleServiceImpl.class);
+ private static final Logger LOGGER = LoggerFactory.getLogger(TreblleServiceImpl.class);
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
@@ -71,6 +76,12 @@ public TreblleServiceImpl(Environment environment, TreblleProperties treblleProp
throw new IllegalStateException("Treblle Project ID is required.");
}
+ if (treblleProperties.isDebug()) {
+ restTemplateBuilder = restTemplateBuilder
+ .requestFactory(() -> new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()))
+ .additionalInterceptors(new TreblleLoggingInterceptor());
+ }
+
this.restTemplate =
restTemplateBuilder
.setConnectTimeout(Duration.ofSeconds(1))
@@ -182,10 +193,12 @@ public void maskAndSendPayload(TrebllePayload payload, byte[] requestBody, byte[
try {
restTemplate.postForEntity(Optional.ofNullable(treblleProperties.getEndpoint()).orElse(getRandomAPIEndpoint()), requestEntity, Void.class);
} catch (RestClientException exception) {
- log.error("An error occurred while sending network request to Treblle.", exception);
+ if (treblleProperties.isDebug()) {
+ LOGGER.error("An error occurred while sending network request to Treblle.", exception);
+ }
}
} catch (Exception exception) {
- log.error("An error occurred while sending data to Treblle.", exception);
+ LOGGER.error("An error occurred while preparing data for Treblle.", exception);
}
}
@@ -214,4 +227,32 @@ private JsonNode readBody(byte[] body, Consumer errorConsumer) {
return null;
}
}
+
+ public static class TreblleLoggingInterceptor implements ClientHttpRequestInterceptor {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(TreblleLoggingInterceptor.class);
+
+ @Override
+ public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
+ LOGGER.debug("Request Method: " + request.getMethod());
+ LOGGER.debug("Request URI: " + request.getURI());
+ LOGGER.debug("Request Headers: " + request.getHeaders());
+ if (body.length > 0) {
+ LOGGER.debug("Request Body: " + new String(body, StandardCharsets.UTF_8));
+ }
+
+ ClientHttpResponse response = execution.execute(request, body);
+
+ LOGGER.debug("Response Status Code: " + response.getStatusCode());
+ LOGGER.debug("Response Headers: " + response.getHeaders());
+ if (response.getBody() != null) {
+ String responseBody = StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8);
+ LOGGER.debug("Response Body: " + responseBody);
+ }
+
+ return response;
+ }
+
+ }
+
}