|
24 | 24 | import com.fasterxml.jackson.databind.node.MissingNode;
|
25 | 25 | import com.fasterxml.jackson.databind.node.NullNode;
|
26 | 26 | import com.fasterxml.jackson.databind.node.ObjectNode;
|
| 27 | +import joptsimple.internal.Strings; |
27 | 28 | import org.apache.commons.lang3.StringUtils;
|
28 | 29 | import org.apache.logging.log4j.Level;
|
29 | 30 | import org.apache.logging.log4j.Marker;
|
|
32 | 33 | import org.apache.logging.log4j.core.config.Configuration;
|
33 | 34 | import org.apache.logging.log4j.core.config.DefaultConfiguration;
|
34 | 35 | import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory;
|
35 |
| -import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration; |
36 | 36 | import org.apache.logging.log4j.core.impl.Log4jLogEvent;
|
37 | 37 | import org.apache.logging.log4j.core.layout.ByteBufferDestination;
|
38 | 38 | import org.apache.logging.log4j.core.lookup.MainMapLookup;
|
@@ -245,7 +245,7 @@ public void test_inline_template() throws Exception {
|
245 | 245 | String eventTemplate = eventTemplateRootNode.toString();
|
246 | 246 |
|
247 | 247 | // Create the layout.
|
248 |
| - BuiltConfiguration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
| 248 | + Configuration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
249 | 249 | String timeZoneId = TimeZone.getTimeZone("Europe/Amsterdam").getID();
|
250 | 250 | LogstashLayout layout = LogstashLayout
|
251 | 251 | .newBuilder()
|
@@ -275,7 +275,7 @@ public void test_log4j_deferred_runtime_resolver_for_MapMessage() throws Excepti
|
275 | 275 | String eventTemplate = eventTemplateRootNode.toString();
|
276 | 276 |
|
277 | 277 | // Create the layout.
|
278 |
| - BuiltConfiguration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
| 278 | + Configuration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
279 | 279 | LogstashLayout layout = LogstashLayout
|
280 | 280 | .newBuilder()
|
281 | 281 | .setConfiguration(configuration)
|
@@ -312,7 +312,7 @@ public void test_MapMessage_serialization() throws Exception {
|
312 | 312 | String eventTemplate = eventTemplateRootNode.toString();
|
313 | 313 |
|
314 | 314 | // Create the layout.
|
315 |
| - BuiltConfiguration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
| 315 | + Configuration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
316 | 316 | LogstashLayout layout = LogstashLayout
|
317 | 317 | .newBuilder()
|
318 | 318 | .setConfiguration(configuration)
|
@@ -403,7 +403,7 @@ public void test_empty_root_cause() throws Exception {
|
403 | 403 | String eventTemplate = eventTemplateRootNode.toString();
|
404 | 404 |
|
405 | 405 | // Create the layout.
|
406 |
| - BuiltConfiguration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
| 406 | + Configuration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
407 | 407 | LogstashLayout layout = LogstashLayout
|
408 | 408 | .newBuilder()
|
409 | 409 | .setConfiguration(configuration)
|
@@ -449,7 +449,7 @@ public void test_root_cause() throws Exception {
|
449 | 449 | String eventTemplate = eventTemplateRootNode.toString();
|
450 | 450 |
|
451 | 451 | // Create the layout.
|
452 |
| - BuiltConfiguration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
| 452 | + Configuration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
453 | 453 | LogstashLayout layout = LogstashLayout
|
454 | 454 | .newBuilder()
|
455 | 455 | .setConfiguration(configuration)
|
@@ -493,7 +493,7 @@ public void test_marker_name() throws IOException {
|
493 | 493 | String eventTemplate = eventTemplateRootNode.toString();
|
494 | 494 |
|
495 | 495 | // Create the layout.
|
496 |
| - BuiltConfiguration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
| 496 | + Configuration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
497 | 497 | LogstashLayout layout = LogstashLayout
|
498 | 498 | .newBuilder()
|
499 | 499 | .setConfiguration(configuration)
|
@@ -529,7 +529,7 @@ public void test_lineSeparator_suffix() {
|
529 | 529 | private void test_lineSeparator_suffix(LogEvent logEvent, boolean prettyPrintEnabled) {
|
530 | 530 |
|
531 | 531 | // Create the layout.
|
532 |
| - BuiltConfiguration config = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
| 532 | + Configuration config = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
533 | 533 | LogstashLayout layout = LogstashLayout
|
534 | 534 | .newBuilder()
|
535 | 535 | .setConfiguration(config)
|
@@ -575,7 +575,7 @@ public void test_main_key_access() throws IOException {
|
575 | 575 | String template = templateRootNode.toString();
|
576 | 576 |
|
577 | 577 | // Create the layout.
|
578 |
| - BuiltConfiguration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
| 578 | + Configuration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
579 | 579 | LogstashLayout layout = LogstashLayout
|
580 | 580 | .newBuilder()
|
581 | 581 | .setConfiguration(configuration)
|
@@ -627,7 +627,7 @@ public void test_mdc_key_access() throws IOException {
|
627 | 627 | String eventTemplate = eventTemplateRootNode.toString();
|
628 | 628 |
|
629 | 629 | // Create the layout.
|
630 |
| - BuiltConfiguration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
| 630 | + Configuration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
631 | 631 | LogstashLayout layout = LogstashLayout
|
632 | 632 | .newBuilder()
|
633 | 633 | .setConfiguration(configuration)
|
@@ -665,7 +665,7 @@ public void test_MapResolver() throws IOException {
|
665 | 665 | String eventTemplate = eventTemplateRootNode.toString();
|
666 | 666 |
|
667 | 667 | // Create the layout.
|
668 |
| - BuiltConfiguration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
| 668 | + Configuration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
669 | 669 | LogstashLayout layout = LogstashLayout
|
670 | 670 | .newBuilder()
|
671 | 671 | .setConfiguration(configuration)
|
@@ -789,7 +789,7 @@ public void test_message_json() throws IOException {
|
789 | 789 | String eventTemplate = eventTemplateRootNode.toString();
|
790 | 790 |
|
791 | 791 | // Create the layout.
|
792 |
| - BuiltConfiguration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
| 792 | + Configuration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
793 | 793 | LogstashLayout layout = LogstashLayout
|
794 | 794 | .newBuilder()
|
795 | 795 | .setConfiguration(configuration)
|
@@ -823,7 +823,7 @@ public void test_message_json_fallback() throws IOException {
|
823 | 823 | String eventTemplate = eventTemplateRootNode.toString();
|
824 | 824 |
|
825 | 825 | // Create the layout.
|
826 |
| - BuiltConfiguration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
| 826 | + Configuration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
827 | 827 | LogstashLayout layout = LogstashLayout
|
828 | 828 | .newBuilder()
|
829 | 829 | .setConfiguration(configuration)
|
@@ -874,7 +874,7 @@ public void test_message_object() throws IOException {
|
874 | 874 | String eventTemplate = eventTemplateRootNode.toString();
|
875 | 875 |
|
876 | 876 | // Create the layout.
|
877 |
| - BuiltConfiguration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
| 877 | + Configuration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
878 | 878 | LogstashLayout layout = LogstashLayout
|
879 | 879 | .newBuilder()
|
880 | 880 | .setConfiguration(configuration)
|
@@ -912,7 +912,7 @@ public void test_StackTraceElement_template() throws IOException {
|
912 | 912 | String eventTemplate = eventTemplateRootNode.toString();
|
913 | 913 |
|
914 | 914 | // Create the layout.
|
915 |
| - BuiltConfiguration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
| 915 | + Configuration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
916 | 916 | LogstashLayout layout = LogstashLayout
|
917 | 917 | .newBuilder()
|
918 | 918 | .setConfiguration(configuration)
|
@@ -956,7 +956,7 @@ public void test_StackTraceElement_template() throws IOException {
|
956 | 956 | public void test_toSerializable_toByteArray_encode_outputs() {
|
957 | 957 |
|
958 | 958 | // Create the layout.
|
959 |
| - BuiltConfiguration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
| 959 | + Configuration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
960 | 960 | LogstashLayout layout = LogstashLayout
|
961 | 961 | .newBuilder()
|
962 | 962 | .setConfiguration(configuration)
|
@@ -1035,7 +1035,7 @@ public void test_maxStringLength() throws IOException {
|
1035 | 1035 | String eventTemplate = eventTemplateRootNode.toString();
|
1036 | 1036 |
|
1037 | 1037 | // Create the layout.
|
1038 |
| - BuiltConfiguration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
| 1038 | + Configuration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
1039 | 1039 | LogstashLayout layout = LogstashLayout
|
1040 | 1040 | .newBuilder()
|
1041 | 1041 | .setConfiguration(configuration)
|
@@ -1098,7 +1098,7 @@ public void test_exception_with_nonAscii_utf8_method_name() throws IOException {
|
1098 | 1098 | String eventTemplate = eventTemplateRootNode.toString();
|
1099 | 1099 |
|
1100 | 1100 | // Create the layout.
|
1101 |
| - BuiltConfiguration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
| 1101 | + Configuration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
1102 | 1102 | LogstashLayout layout = LogstashLayout
|
1103 | 1103 | .newBuilder()
|
1104 | 1104 | .setConfiguration(configuration)
|
@@ -1133,7 +1133,7 @@ public void test_custom_ObjectMapper_factory_method() throws IOException {
|
1133 | 1133 | String eventTemplate = eventTemplateRootNode.toString();
|
1134 | 1134 |
|
1135 | 1135 | // Create the layout.
|
1136 |
| - BuiltConfiguration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
| 1136 | + Configuration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
1137 | 1137 | LogstashLayout layout = LogstashLayout
|
1138 | 1138 | .newBuilder()
|
1139 | 1139 | .setObjectMapperFactoryMethod("com.vlkan.log4j2.logstash.layout.LogstashLayoutTest.getCustomObjectMapper")
|
@@ -1175,7 +1175,7 @@ public void test_event_template_additional_fields() throws IOException {
|
1175 | 1175 | String eventTemplate = eventTemplateRootNode.toString();
|
1176 | 1176 |
|
1177 | 1177 | // Create the layout.
|
1178 |
| - BuiltConfiguration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
| 1178 | + Configuration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
1179 | 1179 | KeyValuePair additionalField1 = new KeyValuePair("message", "${json:message}");
|
1180 | 1180 | KeyValuePair additionalField2 = new KeyValuePair("@version", "1");
|
1181 | 1181 | KeyValuePair[] additionalFieldPairs = {additionalField1, additionalField2};
|
@@ -1236,7 +1236,7 @@ public void test_timestamp_divisor() throws IOException {
|
1236 | 1236 | String eventTemplate = eventTemplateRootNode.toString();
|
1237 | 1237 |
|
1238 | 1238 | // Create the layout.
|
1239 |
| - BuiltConfiguration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
| 1239 | + Configuration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
1240 | 1240 | LogstashLayout layout = LogstashLayout
|
1241 | 1241 | .newBuilder()
|
1242 | 1242 | .setConfiguration(configuration)
|
@@ -1272,7 +1272,7 @@ public void test_level_severity() throws IOException {
|
1272 | 1272 | String eventTemplate = eventTemplateRootNode.toString();
|
1273 | 1273 |
|
1274 | 1274 | // Create the layout.
|
1275 |
| - BuiltConfiguration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
| 1275 | + Configuration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
1276 | 1276 | LogstashLayout layout = LogstashLayout
|
1277 | 1277 | .newBuilder()
|
1278 | 1278 | .setConfiguration(configuration)
|
@@ -1324,7 +1324,7 @@ public void test_exception_resolvers_against_no_exceptions() throws IOException
|
1324 | 1324 | String eventTemplate = eventTemplateRootNode.toString();
|
1325 | 1325 |
|
1326 | 1326 | // Create the layout.
|
1327 |
| - BuiltConfiguration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
| 1327 | + Configuration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
1328 | 1328 | LogstashLayout layout = LogstashLayout
|
1329 | 1329 | .newBuilder()
|
1330 | 1330 | .setConfiguration(configuration)
|
@@ -1363,7 +1363,7 @@ public void test_timestamp_resolver() throws IOException {
|
1363 | 1363 | String eventTemplate = eventTemplateRootNode.toString();
|
1364 | 1364 |
|
1365 | 1365 | // Create the layout.
|
1366 |
| - BuiltConfiguration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
| 1366 | + Configuration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
1367 | 1367 | LogstashLayout layout = LogstashLayout
|
1368 | 1368 | .newBuilder()
|
1369 | 1369 | .setConfiguration(configuration)
|
@@ -1442,4 +1442,39 @@ public void test_StackTraceTextResolver_with_maxStringLength() throws Exception
|
1442 | 1442 |
|
1443 | 1443 | }
|
1444 | 1444 |
|
| 1445 | + @Test |
| 1446 | + public void test_maxStringLength_with_emptyPropertyExclusionEnabled() throws Exception { |
| 1447 | + |
| 1448 | + // Create the event template. |
| 1449 | + ObjectNode eventTemplateRootNode = JSON_NODE_FACTORY.objectNode(); |
| 1450 | + eventTemplateRootNode.put("message", "${json:message}"); |
| 1451 | + String eventTemplate = eventTemplateRootNode.toString(); |
| 1452 | + |
| 1453 | + // Create the layout. |
| 1454 | + int maxStringLength = eventTemplate.length(); |
| 1455 | + Configuration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build(); |
| 1456 | + LogstashLayout layout = LogstashLayout |
| 1457 | + .newBuilder() |
| 1458 | + .setConfiguration(configuration) |
| 1459 | + .setEventTemplate(eventTemplate) |
| 1460 | + .setMaxStringLength(maxStringLength) |
| 1461 | + .setEmptyPropertyExclusionEnabled(true) |
| 1462 | + .build(); |
| 1463 | + |
| 1464 | + // Create the log event. |
| 1465 | + SimpleMessage message = new SimpleMessage(Strings.repeat('m', maxStringLength) + 'x'); |
| 1466 | + LogEvent logEvent = Log4jLogEvent |
| 1467 | + .newBuilder() |
| 1468 | + .setLoggerName(LogstashLayoutTest.class.getSimpleName()) |
| 1469 | + .setMessage(message) |
| 1470 | + .build(); |
| 1471 | + |
| 1472 | + // Check the serialized event. |
| 1473 | + String serializedLogEvent = layout.toSerializable(logEvent); |
| 1474 | + JsonNode rootNode = OBJECT_MAPPER.readTree(serializedLogEvent); |
| 1475 | + String expectedMessage = message.getFormattedMessage().substring(0, maxStringLength); |
| 1476 | + assertThat(point(rootNode, "message").asText()).isEqualTo(expectedMessage); |
| 1477 | + |
| 1478 | + } |
| 1479 | + |
1445 | 1480 | }
|
0 commit comments