Skip to content

Commit 9ebf470

Browse files
committed
perf(log): don't pretty-print request/response bodies longer than 500 characters
1 parent 2d4f83f commit 9ebf470

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/main/java/com/conveyal/datatools/common/utils/SparkUtils.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class SparkUtils {
3030
private static final ObjectMapper mapper = new ObjectMapper();
3131
private static final String BASE_URL = getConfigPropertyAsText("application.public_url");
3232
private static final int DEFAULT_LINES_TO_PRINT = 10;
33+
private static final int MAX_CHARACTERS_TO_PRINT = 500;
3334

3435
/**
3536
* Write out the supplied file to the Spark response as an octet-stream.
@@ -162,13 +163,19 @@ public static void logRequestOrResponse(boolean logRequest, Request request, Res
162163
}
163164
if ("application/json".equals(contentType)) {
164165
bodyString = logRequest ? request.body() : response.body();
165-
if (bodyString != null) {
166+
if (bodyString == null) {
167+
bodyString = "{body content is null}";
168+
} else if (bodyString.length() > MAX_CHARACTERS_TO_PRINT) {
169+
bodyString = new StringBuilder()
170+
.append("body content is longer than 500 characters, printing first 500 characters here:\n")
171+
.append(bodyString, 0, MAX_CHARACTERS_TO_PRINT)
172+
.append("\n...and " + (bodyString.length() - MAX_CHARACTERS_TO_PRINT) + " more characters")
173+
.toString();
174+
} else {
166175
// Pretty print JSON if ContentType is JSON and body is not empty
167176
JsonNode jsonNode = mapper.readTree(bodyString);
168177
// Add new line for legibility when printing
169178
bodyString = "\n" + mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonNode);
170-
} else {
171-
bodyString = "{body content is null}";
172179
}
173180
} else if (contentType != null) {
174181
bodyString = String.format("\nnon-JSON body type: %s", contentType);

0 commit comments

Comments
 (0)