aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/main/java/org/onap/vid/utils/Logging.java
diff options
context:
space:
mode:
authorIttay Stern <ittay.stern@att.com>2020-04-07 13:27:03 +0300
committerIttay Stern <ittay.stern@att.com>2020-04-07 16:14:01 +0300
commitef99ea6b4b4a5b62a31d0ad4b08233620df14a87 (patch)
tree7c26de21b09cf460f120520b1aaa68cd03b737fe /vid-app-common/src/main/java/org/onap/vid/utils/Logging.java
parent49aca2f796200b2e4bdc16aababc7419932a7f6a (diff)
Limit the length of logged responses in outgoingRequestLog
Currently truncated after 1 MB. Issue-ID: VID-804 Change-Id: Ic99c33eede488db60c296d7272850cadee9f2925 Signed-off-by: Ittay Stern <ittay.stern@att.com>
Diffstat (limited to 'vid-app-common/src/main/java/org/onap/vid/utils/Logging.java')
-rw-r--r--vid-app-common/src/main/java/org/onap/vid/utils/Logging.java25
1 files changed, 23 insertions, 2 deletions
diff --git a/vid-app-common/src/main/java/org/onap/vid/utils/Logging.java b/vid-app-common/src/main/java/org/onap/vid/utils/Logging.java
index f9894d1aa..ce811b41c 100644
--- a/vid-app-common/src/main/java/org/onap/vid/utils/Logging.java
+++ b/vid-app-common/src/main/java/org/onap/vid/utils/Logging.java
@@ -35,6 +35,7 @@ import io.joshworks.restclient.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Map;
+import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.Callable;
@@ -122,7 +123,7 @@ public class Logging {
}
try {
response.bufferEntity();
- logger.debug("Received {} {} Status: {} . Body: {}", method.name(), url, response.getStatus(), response.readEntity(entityClass));
+ logger.debug("Received {} {} Status: {} . Body: {}", method.name(), url, response.getStatus(), new Substring(response.readEntity(entityClass)));
}
catch (Exception e) {
logger.debug("Received {} {} Status: {} . Failed to read response as {}", method.name(), url, response.getStatus(), entityClass.getName());
@@ -132,7 +133,7 @@ public class Logging {
public <T> void logResponse(final EELFLogger logger, final HttpMethod method, final String url, final HttpResponse<T> response) {
try {
logger.debug("Received {} {} Status: {} . Body: {}", method.name(),
- url, response.getStatus(), IOUtils.toString(response.getRawBody(), StandardCharsets.UTF_8));
+ url, response.getStatus(), new Substring(IOUtils.toString(response.getRawBody(), StandardCharsets.UTF_8)));
response.getRawBody().reset();
}
catch (Exception e) {
@@ -204,6 +205,26 @@ public class Logging {
}
/**
+ * This class defers the toString() and truncation to the point in time where logger needs it.
+ * This will save some bytes in memory if logger will decide to discard the logging (mostly because logging level
+ * is filtering the message out).
+ */
+ static class Substring {
+ private final Object obj;
+ private final int maxLen = 1_000_000;
+
+ public Substring(Object obj) {
+ this.obj = obj;
+ }
+
+ @Override
+ public String toString() {
+ // null safe truncation
+ return StringUtils.left(Objects.toString(obj), maxLen);
+ }
+ }
+
+ /**
* in order to be able to write the correct data while creating the node on a new thread save a copy of the current
* thread's context map, with keys and values of type String.
*/