Skip to content

Commit da1a546

Browse files
Merge pull request #126 from SAP/issue/124
Fix invalid JSON messages
2 parents 7be41b4 + 0f48e2d commit da1a546

File tree

4 files changed

+8
-14
lines changed

4 files changed

+8
-14
lines changed

cf-java-logging-support-log4j2/src/main/java/com/sap/hcp/cf/log4j2/converter/ContextPropsConverter.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ public void format(LogEvent event, StringBuilder toAppendTo) {
5151
addCustomFieldsFromArguments(contextData, event);
5252
int lengthBefore = toAppendTo.length();
5353
converter.convert(toAppendTo, contextData);
54-
// append comma only, if at least one field was added
55-
// otherwise, there will be to commas ",," rendering the JSON invalid
56-
if (toAppendTo.length() > lengthBefore) {
57-
toAppendTo.append(",");
54+
// remove comma from pattern, when no properties are added
55+
// this is to avoid a double comma in the JSON
56+
// Do not do this on empty messages
57+
if (toAppendTo.length() == lengthBefore && lengthBefore > 0 && toAppendTo.charAt(lengthBefore - 1) == ',') {
58+
toAppendTo.setLength(lengthBefore - 1);
5859
}
5960
}
6061

cf-java-logging-support-log4j2/src/main/java/com/sap/hcp/cf/log4j2/layout/LayoutPatternBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public LayoutPatternBuilder addContextProperties(List<String> exclusions) {
5656
sb.append("%").append(ContextPropsConverter.WORD);
5757
appendParameters(asList(Boolean.toString(sendDefaultValues)));
5858
appendParameters(exclusions);
59+
sb.append(",");
5960
return this;
6061
}
6162

cf-java-logging-support-log4j2/src/test/java/com/sap/hcp/cf/log4j2/converter/ContextPropsConverterTest.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
import java.io.IOException;
1010
import java.util.Map;
1111

12-
import org.apache.logging.log4j.core.LogEvent;
13-
import org.apache.logging.log4j.core.pattern.LogEventPatternConverter;
1412
import org.junit.Test;
1513
import org.slf4j.MDC;
1614

@@ -107,10 +105,4 @@ public void testRegisteredCustomField() throws Exception {
107105
not(hasEntry(SOME_KEY, SOME_VALUE)));
108106
}
109107

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

cf-java-logging-support-log4j2/src/test/java/com/sap/hcp/cf/log4j2/layout/LayoutPatternBuilderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public void applicationScenario() throws Exception {
107107
.suppressExceptions().build();
108108

109109
assertThat(pattern, specificPart(is(
110-
",\"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} ")));
110+
",\"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} ")));
111111
}
112112

113113
@Test
@@ -117,7 +117,7 @@ public void exceptionScenario() throws Exception {
117117
.build();
118118

119119
assertThat(pattern, specificPart(is(
120-
",\"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")));
120+
",\"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")));
121121
}
122122

123123
private static Matcher<String> specificPart(Matcher<String> expected) {

0 commit comments

Comments
 (0)