Skip to content

Commit

Permalink
Only log JSON and XML bodies with logbook (#69)
Browse files Browse the repository at this point in the history
* Only log JSON and XML bodies with logbook

* Move static method
  • Loading branch information
skjolber authored Jan 18, 2025
1 parent 0e2614f commit dfc73cc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 35 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 = 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 = 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
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,6 @@

public abstract class AbstractLogLevelLogstashLogbackSink extends AbstractLogLevelSink {

public static boolean isXmlMediaType(@Nullable final String contentType) {
if (contentType == null) {
return false;
}

String contentTypeWithoutEncoding;
// text/xml;charset=UTF-8
int index = contentType.indexOf(';');
if(index == -1) {
contentTypeWithoutEncoding = contentType;
} else {
contentTypeWithoutEncoding = contentType.substring(0, index).trim();
}

final String lowerCasedContentType = contentTypeWithoutEncoding.toLowerCase();

boolean isApplicationOrText = lowerCasedContentType.startsWith("application/") || lowerCasedContentType.startsWith("text/");
if(!isApplicationOrText) {
return false;
}

return lowerCasedContentType.endsWith("+xml") || lowerCasedContentType.endsWith("/xml");
}

protected final MaxSizeJsonFilter maxSizeJsonFilter;
protected final JsonValidator jsonValidator;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,37 @@
import org.slf4j.Marker;
import org.zalando.logbook.*;

import javax.annotation.Nullable;
import java.io.IOException;
import java.util.function.BiConsumer;
import java.util.function.BooleanSupplier;

public abstract class AbstractLogLevelSink implements Sink {

public static boolean isXmlMediaType(@Nullable final String contentType) {
if (contentType == null) {
return false;
}

String contentTypeWithoutEncoding;
// text/xml;charset=UTF-8
int index = contentType.indexOf(';');
if(index == -1) {
contentTypeWithoutEncoding = contentType;
} else {
contentTypeWithoutEncoding = contentType.substring(0, index).trim();
}

final String lowerCasedContentType = contentTypeWithoutEncoding.toLowerCase();

boolean isApplicationOrText = lowerCasedContentType.startsWith("application/") || lowerCasedContentType.startsWith("text/");
if(!isApplicationOrText) {
return false;
}

return lowerCasedContentType.endsWith("+xml") || lowerCasedContentType.endsWith("/xml");
}

protected final BooleanSupplier logLevelEnabled;

protected final BiConsumer<Marker, String> logConsumer;
Expand Down

0 comments on commit dfc73cc

Please sign in to comment.