3
3
import com .fasterxml .jackson .core .JsonProcessingException ;
4
4
import com .fasterxml .jackson .databind .ObjectMapper ;
5
5
import com .microsoft .playwright .APIResponse ;
6
+ import org .apache .commons .lang3 .StringUtils ;
6
7
import org .apache .logging .log4j .LogManager ;
7
8
8
9
@@ -17,32 +18,40 @@ public Logger (final APIResponse response) {
17
18
}
18
19
19
20
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 ()));
28
25
}
29
26
this .log .info ("End of Logs!" );
30
27
}
31
28
32
29
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 ;
44
38
}
45
- this .log .info ("No response body generated!" );
46
- return null ;
47
39
}
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
+ // }
48
57
}
0 commit comments