@@ -30,6 +30,7 @@ public class SparkUtils {
30
30
private static final ObjectMapper mapper = new ObjectMapper ();
31
31
private static final String BASE_URL = getConfigPropertyAsText ("application.public_url" );
32
32
private static final int DEFAULT_LINES_TO_PRINT = 10 ;
33
+ private static final int MAX_CHARACTERS_TO_PRINT = 500 ;
33
34
34
35
/**
35
36
* 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
162
163
}
163
164
if ("application/json" .equals (contentType )) {
164
165
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 {
166
175
// Pretty print JSON if ContentType is JSON and body is not empty
167
176
JsonNode jsonNode = mapper .readTree (bodyString );
168
177
// Add new line for legibility when printing
169
178
bodyString = "\n " + mapper .writerWithDefaultPrettyPrinter ().writeValueAsString (jsonNode );
170
- } else {
171
- bodyString = "{body content is null}" ;
172
179
}
173
180
} else if (contentType != null ) {
174
181
bodyString = String .format ("\n non-JSON body type: %s" , contentType );
0 commit comments