Skip to content

Commit

Permalink
#55 Work around for TokenFilter-vs-Delegate ordering bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
vy committed Oct 29, 2020
1 parent 898abb9 commit 2aa6853
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

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

- Implement work around for FasterXML/jackson-core#609 triggered when
`maxStringLength` is used in combination with
`emptyPropertyExclusionEnabled=true`. (#55)

### (2019-10-15) v0.21

- Add feature comparison matrix.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,13 @@ private static JsonGenerator createJsonGenerator(
if (prettyPrintEnabled) {
jsonGenerator.setPrettyPrinter(PRETTY_PRINTER);
}
if (emptyPropertyExclusionEnabled) {
jsonGenerator = new FilteringGeneratorDelegate(jsonGenerator, NullExcludingTokenFilter.INSTANCE, true, true);
}
if (maxStringLength > 0) {
jsonGenerator = new StringTruncatingGeneratorDelegate(jsonGenerator, maxStringLength);
}
// TokenFilter has to come last due to FasterXML/jackson-core#609.
if (emptyPropertyExclusionEnabled) {
jsonGenerator = new FilteringGeneratorDelegate(jsonGenerator, NullExcludingTokenFilter.INSTANCE, true, true);
}
return jsonGenerator;
} catch (IOException error) {
throw new RuntimeException("failed creating JsonGenerator", error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1420,4 +1420,39 @@ public void test_StackTraceObjectResolver_against_getStackTrace_failures() throw

}

@Test
public void test_maxStringLength_with_emptyPropertyExclusionEnabled() throws Exception {

// Create the event template.
ObjectNode eventTemplateRootNode = JSON_NODE_FACTORY.objectNode();
eventTemplateRootNode.put("message", "${json:message}");
String eventTemplate = eventTemplateRootNode.toString();

// Create the layout.
int maxStringLength = eventTemplate.length();
Configuration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build();
LogstashLayout layout = LogstashLayout
.newBuilder()
.setConfiguration(configuration)
.setEventTemplate(eventTemplate)
.setMaxStringLength(maxStringLength)
.setEmptyPropertyExclusionEnabled(true)
.build();

// Create the log event.
SimpleMessage message = new SimpleMessage(StringUtils.repeat('m', maxStringLength) + 'x');
LogEvent logEvent = Log4jLogEvent
.newBuilder()
.setLoggerName(LogstashLayoutTest.class.getSimpleName())
.setMessage(message)
.build();

// Check the serialized event.
String serializedLogEvent = layout.toSerializable(logEvent);
JsonNode rootNode = OBJECT_MAPPER.readTree(serializedLogEvent);
String expectedMessage = message.getFormattedMessage().substring(0, maxStringLength);
assertThat(point(rootNode, "message").asText()).isEqualTo(expectedMessage);

}

}

0 comments on commit 2aa6853

Please sign in to comment.