use of org.apache.logging.log4j.message.StringMapMessage 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.message.StringMapMessage in project logging-log4j2 by apache.
the class ReadOnlyStringMapResolverTest method test_map_flatten.
@Test
public void test_map_flatten() {
// Create the log event.
final String patternMatchedKey = "mapKey1";
final String patternMatchedValue = "mapValue1";
final String patternMismatchedKey = "mapKey2";
final String patternMismatchedValue = "mapValue2";
final Message message = new StringMapMessage().with(patternMatchedKey, patternMatchedValue).with(patternMismatchedKey, patternMismatchedValue);
final LogEvent logEvent = Log4jLogEvent.newBuilder().setMessage(message).build();
// Check the serialized event.
testReadOnlyStringMapFlatten(patternMatchedKey, patternMatchedValue, patternMismatchedKey, logEvent, "map");
}
use of org.apache.logging.log4j.message.StringMapMessage in project logging-log4j2 by apache.
the class ReadOnlyStringMapResolverTest method test_map_pattern.
@Test
public void test_map_pattern() {
// Create the log event.
final String patternMatchedKey = "mapKey1";
final String patternMatchedValue = "mapValue1";
final String patternMismatchedKey = "mapKey2";
final String patternMismatchedValue = "mapValue2";
final Message message = new StringMapMessage().with(patternMatchedKey, patternMatchedValue).with(patternMismatchedKey, patternMismatchedValue);
final LogEvent logEvent = Log4jLogEvent.newBuilder().setMessage(message).build();
// Check the serialized event.
testReadOnlyStringMapPattern(patternMatchedKey, patternMatchedValue, patternMismatchedKey, logEvent, "map");
}
use of org.apache.logging.log4j.message.StringMapMessage 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();
});
}
use of org.apache.logging.log4j.message.StringMapMessage in project logging-log4j2 by apache.
the class JsonTemplateLayoutTest method test_log4j_deferred_runtime_resolver_for_MapMessage.
@Test
void test_log4j_deferred_runtime_resolver_for_MapMessage() {
// Create the event template.
final String eventTemplate = writeJson(asMap("mapValue3", asMap("$resolver", "message"), "mapValue1", "${map:key1}", "mapValue2", "${map:key2}", "nestedLookupEmptyValue", "${map:noExist:-${map:noExist2:-${map:noExist3:-}}}", "nestedLookupStaticValue", "${map:noExist:-${map:noExist2:-${map:noExist3:-Static Value}}}"));
// 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", "val2").with("key3", Collections.singletonMap("foo", "bar"));
final LogEvent logEvent = Log4jLogEvent.newBuilder().setLoggerName(LOGGER_NAME).setLevel(Level.INFO).setMessage(mapMessage).setTimeMillis(System.currentTimeMillis()).build();
// Check the serialized event.
usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
assertThat(accessor.getString("mapValue1")).isEqualTo("val1");
assertThat(accessor.getString("mapValue2")).isEqualTo("val2");
assertThat(accessor.getString("nestedLookupEmptyValue")).isEmpty();
assertThat(accessor.getString("nestedLookupStaticValue")).isEqualTo("Static Value");
});
}
Aggregations