use of org.apache.logging.log4j.layout.template.json.JsonTemplateLayout in project logging-log4j2 by apache.
the class MessageResolverTest method test_StringMapMessage.
@Test
void test_StringMapMessage() {
// Create the log event.
final StringMapMessage message = new StringMapMessage();
message.put("message", "Hello, World!");
message.put("bottle", "Kickapoo Joy Juice");
final LogEvent logEvent = Log4jLogEvent.newBuilder().setMessage(message).build();
// Create the event template.
final String eventTemplate = writeJson(asMap("message", asMap("$resolver", "message")));
// Create the layout.
final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setStackTraceEnabled(true).setEventTemplate(eventTemplate).build();
// Check the serialized event.
usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
assertThat(accessor.getString(new String[] { "message", "message" })).isEqualTo("Hello, World!");
assertThat(accessor.getString(new String[] { "message", "bottle" })).isEqualTo("Kickapoo Joy Juice");
});
}
use of org.apache.logging.log4j.layout.template.json.JsonTemplateLayout in project logging-log4j2 by apache.
the class MessageResolverTest method test_MapMessage_serialization.
@Test
void test_MapMessage_serialization() {
// Create the event template.
final String eventTemplate = writeJson(asMap("message", asMap("$resolver", "message")));
// Create the layout.
final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).build();
// Create the log event with a MapMessage.
final StringMapMessage mapMessage = new StringMapMessage().with("key1", "val1").with("key2", 0xDEADBEEF).with("key3", Collections.singletonMap("key3.1", "val3.1"));
final LogEvent logEvent = Log4jLogEvent.newBuilder().setMessage(mapMessage).setTimeMillis(System.currentTimeMillis()).build();
// Check the serialized event.
usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
assertThat(accessor.getString(new String[] { "message", "key1" })).isEqualTo("val1");
assertThat(accessor.getInteger(new String[] { "message", "key2" })).isEqualTo(0xDEADBEEF);
assertThat(accessor.getString(new String[] { "message", "key3", "key3.1" })).isEqualTo("val3.1");
});
}
use of org.apache.logging.log4j.layout.template.json.JsonTemplateLayout in project logging-log4j2 by apache.
the class ReadOnlyStringMapResolverTest method test_MapMessage_keyed_access.
@Test
void test_MapMessage_keyed_access() {
// Create the event template.
final String key = "list";
final String eventTemplate = writeJson(asMap("typedValue", asMap("$resolver", "map", "key", key), "stringifiedValue", asMap("$resolver", "map", "key", key, "stringified", true)));
// Create the layout.
final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).build();
// Create the log event with a MapMessage.
final List<Integer> value = Arrays.asList(1, 2);
final StringMapMessage mapMessage = new StringMapMessage().with(key, value);
final LogEvent logEvent = Log4jLogEvent.newBuilder().setMessage(mapMessage).setTimeMillis(System.currentTimeMillis()).build();
// Check the serialized event.
usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
assertThat(accessor.getObject("typedValue")).isEqualTo(value);
assertThat(accessor.getString("stringifiedValue")).isEqualTo(String.valueOf(value));
});
}
use of org.apache.logging.log4j.layout.template.json.JsonTemplateLayout in project logging-log4j2 by apache.
the class ReadOnlyStringMapResolverTest method testReadOnlyStringMapFlatten.
private static void testReadOnlyStringMapFlatten(final String patternMatchedKey, final String patternMatchedValue, final String patternMismatchedKey, final LogEvent logEvent, final String resolverName) {
// Create the event template.
final String prefix = "_map.";
final String eventTemplate = writeJson(asMap("ignoredFieldName", asMap("$resolver", resolverName, "pattern", patternMatchedKey, "flatten", asMap("prefix", prefix))));
// Create the layout.
final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setStackTraceEnabled(true).setEventTemplate(eventTemplate).build();
// Check the serialized event.
usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
assertThat(accessor.getString(prefix + patternMatchedKey)).isEqualTo(patternMatchedValue);
assertThat(accessor.exists(prefix + patternMismatchedKey)).isFalse();
});
}
use of org.apache.logging.log4j.layout.template.json.JsonTemplateLayout in project logging-log4j2 by apache.
the class ReadOnlyStringMapResolverTest method test_MapResolver.
@Test
void test_MapResolver() {
// Create the log event.
final StringMapMessage message = new StringMapMessage().with("key1", "val1");
final LogEvent logEvent = Log4jLogEvent.newBuilder().setMessage(message).build();
// Create the event template node with map values.
final String eventTemplate = writeJson(asMap("mapValue1", asMap("$resolver", "map", "key", "key1"), "mapValue2", asMap("$resolver", "map", "key", "key?")));
// Create the layout.
final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).build();
// Check serialized event.
usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
assertThat(accessor.getString("mapValue1")).isEqualTo("val1");
assertThat(accessor.getString("mapValue2")).isNull();
});
}
Aggregations