33import com .fasterxml .jackson .core .JsonProcessingException ;
44import com .fasterxml .jackson .databind .ObjectMapper ;
55import com .microsoft .playwright .APIResponse ;
6+ import org .apache .commons .lang3 .StringUtils ;
67import org .apache .logging .log4j .LogManager ;
78
89
@@ -17,32 +18,40 @@ public Logger (final APIResponse response) {
1718 }
1819
1920 public void logResponseDetails () {
20- this .log .info ("Logging Response Details....." );
21-
22- this .log .info ("Response Headers: \n {}" , this .response .headers ());
23- this .log .info ("Status Code: {}" , this .response .status ());
24- if (this .response .text () != null && !this .response .text ()
25- .isEmpty () && !this .response .text ()
26- .isBlank ()) {
27- this .log .info ("Response Body: \n {}" , prettyPrintJson (this .response .text ()));
21+ String responseBody = this .response .text ();
22+ if (StringUtils .isNotBlank (responseBody )) {
23+ this .log .info ("Logging Response Details....\n responseHeaders: {}, \n statusCode: {}, \n responseBody: {}" ,
24+ this .response .headers (), this .response .status (), prettyPrintJson (this .response .text ()));
2825 }
2926 this .log .info ("End of Logs!" );
3027 }
3128
3229 private String prettyPrintJson (final String text ) {
33- String prettyPrintJson = "" ;
34- if (text != null && !text .isBlank () && !text .isEmpty ()) {
35- try {
36-
37- final ObjectMapper objectMapper = new ObjectMapper ();
38- final Object jsonObject = objectMapper .readValue (text , Object .class );
39- prettyPrintJson = objectMapper .writerWithDefaultPrettyPrinter ().writeValueAsString (jsonObject );
40- return prettyPrintJson ;
41- } catch (final JsonProcessingException e ) {
42- this .log .error ("Error Printing Pretty Json : {}" , e .getMessage ());
43- }
30+ try {
31+ final ObjectMapper objectMapper = new ObjectMapper ();
32+ final Object jsonObject = objectMapper .readValue (text , Object .class );
33+ return objectMapper .writerWithDefaultPrettyPrinter ()
34+ .writeValueAsString (jsonObject );
35+ } catch (final JsonProcessingException e ) {
36+ this .log .error ("Failed to pretty print JSON: {}" , e .getMessage (), e );
37+ return text ;
4438 }
45- this .log .info ("No response body generated!" );
46- return null ;
4739 }
40+
41+ // private String prettyPrintJson (final String text) {
42+ // String prettyPrintJson = "";
43+ // if (text != null && !text.isBlank() && !text.isEmpty()) {
44+ // try {
45+ //
46+ // final ObjectMapper objectMapper = new ObjectMapper ();
47+ // final Object jsonObject = objectMapper.readValue (text, Object.class);
48+ // prettyPrintJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject);
49+ // return prettyPrintJson;
50+ // } catch (final JsonProcessingException e) {
51+ // this.log.error ("Error Printing Pretty Json : {}", e.getMessage ());
52+ // }
53+ // }
54+ // this.log.info ("No response body generated!");
55+ // return null;
56+ // }
4857}
0 commit comments