Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid JSON messages #126

Merged
merged 1 commit into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ public void format(LogEvent event, StringBuilder toAppendTo) {
addCustomFieldsFromArguments(contextData, event);
int lengthBefore = toAppendTo.length();
converter.convert(toAppendTo, contextData);
// append comma only, if at least one field was added
// otherwise, there will be to commas ",," rendering the JSON invalid
if (toAppendTo.length() > lengthBefore) {
toAppendTo.append(",");
// remove comma from pattern, when no properties are added
// this is to avoid a double comma in the JSON
// Do not do this on empty messages
if (toAppendTo.length() == lengthBefore && lengthBefore > 0 && toAppendTo.charAt(lengthBefore - 1) == ',') {
toAppendTo.setLength(lengthBefore - 1);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public LayoutPatternBuilder addContextProperties(List<String> exclusions) {
sb.append("%").append(ContextPropsConverter.WORD);
appendParameters(asList(Boolean.toString(sendDefaultValues)));
appendParameters(exclusions);
sb.append(",");
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import java.io.IOException;
import java.util.Map;

import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.pattern.LogEventPatternConverter;
import org.junit.Test;
import org.slf4j.MDC;

Expand Down Expand Up @@ -107,10 +105,4 @@ public void testRegisteredCustomField() throws Exception {
not(hasEntry(SOME_KEY, SOME_VALUE)));
}

@Override
protected String format(LogEventPatternConverter cpc, LogEvent event) {
String converted = super.format(cpc, event);
return converted.length() > 0 ? converted.substring(0, converted.length() - 1) : converted;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void applicationScenario() throws Exception {
.suppressExceptions().build();

assertThat(pattern, specificPart(is(
",\"type\":\"log\",\"logger\":\"%replace{%logger}{\"}{\\\\\"}\",\"thread\":\"%replace{%thread}{\"}{\\\\\"}\",\"level\":\"%p\",\"categories\":%categories,\"msg\":%jsonmsg{escape},%ctxp{false}{excluded-field}\"#cf\":{%cf{custom-field}}%ex{0} ")));
",\"type\":\"log\",\"logger\":\"%replace{%logger}{\"}{\\\\\"}\",\"thread\":\"%replace{%thread}{\"}{\\\\\"}\",\"level\":\"%p\",\"categories\":%categories,\"msg\":%jsonmsg{escape},%ctxp{false}{excluded-field},\"#cf\":{%cf{custom-field}}%ex{0} ")));
}

@Test
Expand All @@ -117,7 +117,7 @@ public void exceptionScenario() throws Exception {
.build();

assertThat(pattern, specificPart(is(
",\"type\":\"log\",\"logger\":\"%replace{%logger}{\"}{\\\\\"}\",\"thread\":\"%replace{%thread}{\"}{\\\\\"}\",\"level\":\"%p\",\"categories\":%categories,\"msg\":%jsonmsg{escape},%ctxp{false}{excluded-field}\"#cf\":{%cf{custom-field}},\"stacktrace\":%stacktrace")));
",\"type\":\"log\",\"logger\":\"%replace{%logger}{\"}{\\\\\"}\",\"thread\":\"%replace{%thread}{\"}{\\\\\"}\",\"level\":\"%p\",\"categories\":%categories,\"msg\":%jsonmsg{escape},%ctxp{false}{excluded-field},\"#cf\":{%cf{custom-field}},\"stacktrace\":%stacktrace")));
}

private static Matcher<String> specificPart(Matcher<String> expected) {
Expand Down