Skip to content

Commit

Permalink
Merge pull request #550 from scireum/feature/fha/SIRI-1085-XML-Format…
Browse files Browse the repository at this point in the history
…ted-Logging

SIRI-1085: Fine Log XML Responses pretty printed
  • Loading branch information
fhaScireum authored Feb 24, 2025
2 parents 0e5d386 + d01c825 commit 40e23fe
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/main/java/sirius/kernel/xml/XMLCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private InputStream getInputStream() throws IOException {
// log the request, even when parsing fails
try (InputStream body = getResponseBody()) {
byte[] bytes = body.readAllBytes();
logRequest(new String(bytes, outcall.getContentEncoding()));
logRequest(bytes);
return new ByteArrayInputStream(bytes);
}
}
Expand All @@ -214,15 +214,15 @@ protected InputStream getResponseBody() throws IOException {
return outcall.getResponse().body();
}

private void logRequest(String response) throws IOException {
private void logRequest(byte[] response) throws IOException {
debugLogger.FINE(Formatter.create("""
---------- call ----------
${httpMethod} ${url} [
${callBody}]
---------- response ----------
HTTP-Response-Code: ${responseCode}
${response}
---------- end ----------
""")
Expand All @@ -231,7 +231,16 @@ private void logRequest(String response) throws IOException {
.set("callBody",
outcall.getRequest().bodyPublisher().isPresent() ? getOutput() : null)
.set("responseCode", getOutcall().getResponseCode())
.set("response", response)
.set("response", formatResponseForLogging(response))
.smartFormat());
}

private String formatResponseForLogging(byte[] response) {
try {
return new XMLStructuredInput(new ByteArrayInputStream(response), namespaceContext).toString();
} catch (Exception exception) {
Exceptions.ignore(exception);
return new String(response, outcall.getContentEncoding());
}
}
}

0 comments on commit 40e23fe

Please sign in to comment.