Search in sources :

Example 21 with StringMapMessage

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));
    });
}
Also used : StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) JsonTemplateLayout(org.apache.logging.log4j.layout.template.json.JsonTemplateLayout) Test(org.junit.jupiter.api.Test)

Example 22 with StringMapMessage

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");
}
Also used : StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Message(org.apache.logging.log4j.message.Message) StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) Test(org.junit.jupiter.api.Test)

Example 23 with StringMapMessage

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");
}
Also used : StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Message(org.apache.logging.log4j.message.Message) StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) Test(org.junit.jupiter.api.Test)

Example 24 with StringMapMessage

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();
    });
}
Also used : StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) JsonTemplateLayout(org.apache.logging.log4j.layout.template.json.JsonTemplateLayout) Test(org.junit.jupiter.api.Test)

Example 25 with StringMapMessage

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");
    });
}
Also used : StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) Test(org.junit.jupiter.api.Test)

Aggregations

StringMapMessage (org.apache.logging.log4j.message.StringMapMessage)25 Test (org.junit.jupiter.api.Test)23 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)21 LogEvent (org.apache.logging.log4j.core.LogEvent)20 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)8 HashMap (java.util.HashMap)7 Message (org.apache.logging.log4j.message.Message)7 JsonTemplateLayout (org.apache.logging.log4j.layout.template.json.JsonTemplateLayout)5 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)4 Logger (org.apache.logging.log4j.Logger)3 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)3 KeyValuePair (org.apache.logging.log4j.core.util.KeyValuePair)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Collection (java.util.Collection)1 Filter (org.apache.logging.log4j.core.Filter)1 LoggerContextSource (org.apache.logging.log4j.core.test.junit.LoggerContextSource)1 MapMessageLookup (org.apache.logging.log4j.lookup.MapMessageLookup)1 MapMessage (org.apache.logging.log4j.message.MapMessage)1 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)1