Skip to content

Commit

Permalink
Only log JSON and XML bodies with logbook
Browse files Browse the repository at this point in the history
  • Loading branch information
skjolber committed Jan 18, 2025
1 parent 0e2614f commit fde70ae
Showing 1 changed file with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import no.entur.logging.cloud.logbook.AbstractLogLevelLogstashLogbackSink;
import org.entur.jackson.jsh.AnsiSyntaxHighlight;
import org.entur.jackson.jsh.SyntaxHighlighter;
import org.entur.jackson.jsh.SyntaxHighlightingJsonGenerator;
Expand Down Expand Up @@ -68,27 +69,35 @@ public PrettyPrintingSink(BooleanSupplier logLevelEnabled, BiConsumer<Marker, St
protected void requestMessage(HttpRequest request, StringBuilder messageBuilder) throws IOException {
super.requestMessage(request, messageBuilder);

final String body = request.getBodyAsString();

messageBuilder.append('\n');
writeHeaders(request.getHeaders(), messageBuilder);
writeBody(body, request.getContentType(), messageBuilder);

String contentType = request.getContentType();
boolean isJson = ContentType.isJsonMediaType(contentType);
boolean isXml = AbstractLogLevelLogstashLogbackSink.isXmlMediaType(contentType);

if(isJson || isXml) {
final String body = request.getBodyAsString();
writeBody(body, contentType, messageBuilder);
}
}

@Override
protected void responseMessage(Correlation correlation, HttpRequest request, HttpResponse response, StringBuilder messageBuilder) throws IOException {
super.responseMessage(correlation, request, response, messageBuilder);

final String body = response.getBodyAsString();

messageBuilder.append('\n');
writeHeaders(response.getHeaders(), messageBuilder);
if(body != null) {
writeBody(body, response.getContentType(), messageBuilder);
}

}
String contentType = response.getContentType();
boolean isJson = ContentType.isJsonMediaType(contentType);
boolean isXml = AbstractLogLevelLogstashLogbackSink.isXmlMediaType(contentType);

if(isJson || isXml) {
final String body = response.getBodyAsString();
writeBody(body, contentType, messageBuilder);
}
}

private void writeHeaders(final Map<String, List<String>> headers, final StringBuilder output) {
if (headers.isEmpty()) {
Expand Down Expand Up @@ -118,9 +127,11 @@ private void writeHeaders(final Map<String, List<String>> headers, final StringB
}

private void writeBody(final String body, String contentType, final StringBuilder output) {
if (!body.isEmpty()) {
if (body != null && !body.isEmpty()) {
output.append('\n');
if(ContentType.isJsonMediaType(contentType)) {

boolean isJson = ContentType.isJsonMediaType(contentType);
if(isJson) {
output.append(prettyPrint(body));
} else {
output.append(body);
Expand Down

0 comments on commit fde70ae

Please sign in to comment.