Search in sources :

Example 6 with JsonTemplateLayout

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");
    });
}
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 7 with JsonTemplateLayout

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");
    });
}
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 8 with JsonTemplateLayout

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));
    });
}
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 9 with JsonTemplateLayout

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();
    });
}
Also used : JsonTemplateLayout(org.apache.logging.log4j.layout.template.json.JsonTemplateLayout)

Example 10 with JsonTemplateLayout

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();
    });
}
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)

Aggregations

JsonTemplateLayout (org.apache.logging.log4j.layout.template.json.JsonTemplateLayout)14 LogEvent (org.apache.logging.log4j.core.LogEvent)10 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)10 Test (org.junit.jupiter.api.Test)8 StringMapMessage (org.apache.logging.log4j.message.StringMapMessage)5 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)2 SortedArrayStringMap (org.apache.logging.log4j.util.SortedArrayStringMap)2 StringMap (org.apache.logging.log4j.util.StringMap)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 CsvSource (org.junit.jupiter.params.provider.CsvSource)2 Field (java.lang.reflect.Field)1 LinkedHashMap (java.util.LinkedHashMap)1 LoggerContextSource (org.apache.logging.log4j.core.test.junit.LoggerContextSource)1 Message (org.apache.logging.log4j.message.Message)1 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)1 MpmcArrayQueue (org.jctools.queues.MpmcArrayQueue)1