Skip to content

Commit 2aa6853

Browse files
committed
#55 Work around for TokenFilter-vs-Delegate ordering bug.
1 parent 898abb9 commit 2aa6853

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
- Fix NPE in `StringTruncatingGeneratorDelegate`. (#53)
88

9+
- Implement work around for FasterXML/jackson-core#609 triggered when
10+
`maxStringLength` is used in combination with
11+
`emptyPropertyExclusionEnabled=true`. (#55)
12+
913
### (2019-10-15) v0.21
1014

1115
- Add feature comparison matrix.

layout/src/main/java/com/vlkan/log4j2/logstash/layout/LogstashLayoutSerializationContexts.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,13 @@ private static JsonGenerator createJsonGenerator(
9191
if (prettyPrintEnabled) {
9292
jsonGenerator.setPrettyPrinter(PRETTY_PRINTER);
9393
}
94-
if (emptyPropertyExclusionEnabled) {
95-
jsonGenerator = new FilteringGeneratorDelegate(jsonGenerator, NullExcludingTokenFilter.INSTANCE, true, true);
96-
}
9794
if (maxStringLength > 0) {
9895
jsonGenerator = new StringTruncatingGeneratorDelegate(jsonGenerator, maxStringLength);
9996
}
97+
// TokenFilter has to come last due to FasterXML/jackson-core#609.
98+
if (emptyPropertyExclusionEnabled) {
99+
jsonGenerator = new FilteringGeneratorDelegate(jsonGenerator, NullExcludingTokenFilter.INSTANCE, true, true);
100+
}
100101
return jsonGenerator;
101102
} catch (IOException error) {
102103
throw new RuntimeException("failed creating JsonGenerator", error);

layout/src/test/java/com/vlkan/log4j2/logstash/layout/LogstashLayoutTest.java

+35
Original file line numberDiff line numberDiff line change
@@ -1420,4 +1420,39 @@ public void test_StackTraceObjectResolver_against_getStackTrace_failures() throw
14201420

14211421
}
14221422

1423+
@Test
1424+
public void test_maxStringLength_with_emptyPropertyExclusionEnabled() throws Exception {
1425+
1426+
// Create the event template.
1427+
ObjectNode eventTemplateRootNode = JSON_NODE_FACTORY.objectNode();
1428+
eventTemplateRootNode.put("message", "${json:message}");
1429+
String eventTemplate = eventTemplateRootNode.toString();
1430+
1431+
// Create the layout.
1432+
int maxStringLength = eventTemplate.length();
1433+
Configuration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build();
1434+
LogstashLayout layout = LogstashLayout
1435+
.newBuilder()
1436+
.setConfiguration(configuration)
1437+
.setEventTemplate(eventTemplate)
1438+
.setMaxStringLength(maxStringLength)
1439+
.setEmptyPropertyExclusionEnabled(true)
1440+
.build();
1441+
1442+
// Create the log event.
1443+
SimpleMessage message = new SimpleMessage(StringUtils.repeat('m', maxStringLength) + 'x');
1444+
LogEvent logEvent = Log4jLogEvent
1445+
.newBuilder()
1446+
.setLoggerName(LogstashLayoutTest.class.getSimpleName())
1447+
.setMessage(message)
1448+
.build();
1449+
1450+
// Check the serialized event.
1451+
String serializedLogEvent = layout.toSerializable(logEvent);
1452+
JsonNode rootNode = OBJECT_MAPPER.readTree(serializedLogEvent);
1453+
String expectedMessage = message.getFormattedMessage().substring(0, maxStringLength);
1454+
assertThat(point(rootNode, "message").asText()).isEqualTo(expectedMessage);
1455+
1456+
}
1457+
14231458
}

0 commit comments

Comments
 (0)