Search in sources :

Example 11 with JsonTemplateLayout

use of org.apache.logging.log4j.layout.template.json.JsonTemplateLayout in project logging-log4j2 by apache.

the class ReadOnlyStringMapResolverTest method testReadOnlyStringMapPattern.

private static void testReadOnlyStringMapPattern(final String patternMatchedKey, final String patternMatchedValue, final String patternMismatchedKey, final LogEvent logEvent, final String resolverName) {
    // Create the event template.
    final String mapFieldName = "map";
    final String eventTemplate = writeJson(asMap(mapFieldName, asMap("$resolver", resolverName, "pattern", patternMatchedKey)));
    // 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[] { mapFieldName, patternMatchedKey })).isEqualTo(patternMatchedValue);
        assertThat(accessor.exists(new String[] { mapFieldName, patternMismatchedKey })).isFalse();
    });
}
Also used : JsonTemplateLayout(org.apache.logging.log4j.layout.template.json.JsonTemplateLayout)

Example 12 with JsonTemplateLayout

use of org.apache.logging.log4j.layout.template.json.JsonTemplateLayout in project logging-log4j2 by apache.

the class ReadOnlyStringMapResolverTest method testReadOnlyStringMapKeyAccess.

private static void testReadOnlyStringMapKeyAccess(final String directlyAccessedKey, final String directlyAccessedValue, final String directlyAccessedNullPropertyKey, final LogEvent logEvent, final String resolverName) {
    // Create the event template.
    String eventTemplate = writeJson(asMap(directlyAccessedKey, asMap("$resolver", resolverName, "key", directlyAccessedKey), directlyAccessedNullPropertyKey, asMap("$resolver", resolverName, "key", directlyAccessedNullPropertyKey)));
    // 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(directlyAccessedKey)).isEqualTo(directlyAccessedValue);
        assertThat(accessor.getString(directlyAccessedNullPropertyKey)).isNull();
    });
}
Also used : JsonTemplateLayout(org.apache.logging.log4j.layout.template.json.JsonTemplateLayout)

Example 13 with JsonTemplateLayout

use of org.apache.logging.log4j.layout.template.json.JsonTemplateLayout in project logging-log4j2 by apache.

the class ReadOnlyStringMapResolverTest method pattern_replacement_should_work.

@Test
void pattern_replacement_should_work() {
    // Create the event template.
    final String eventTemplate = writeJson(asMap("$resolver", "mdc", "pattern", "user:(role|rank)", "replacement", "$1"));
    // Create the layout.
    final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).build();
    // Create the log event.
    final StringMap contextData = new SortedArrayStringMap();
    contextData.putValue("user:role", "engineer");
    contextData.putValue("user:rank", "senior");
    final LogEvent logEvent = Log4jLogEvent.newBuilder().setContextData(contextData).build();
    // Check the serialized event.
    usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
        assertThat(accessor.getString("role")).isEqualTo("engineer");
        assertThat(accessor.getString("rank")).isEqualTo("senior");
    });
}
Also used : SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) StringMap(org.apache.logging.log4j.util.StringMap) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) JsonTemplateLayout(org.apache.logging.log4j.layout.template.json.JsonTemplateLayout) Test(org.junit.jupiter.api.Test)

Example 14 with JsonTemplateLayout

use of org.apache.logging.log4j.layout.template.json.JsonTemplateLayout in project logging-log4j2 by apache.

the class RecyclerFactoriesTest method test_RecyclerFactoryConverter_using_XML_config.

@Test
@LoggerContextSource("recyclerFactoryCustomizedJsonTemplateLayoutLogging.xml")
void test_RecyclerFactoryConverter_using_XML_config(@Named(value = "List") final ListAppender appender) throws Exception {
    final JsonTemplateLayout layout = (JsonTemplateLayout) appender.getLayout();
    final Field field = JsonTemplateLayout.class.getDeclaredField("contextRecycler");
    field.setAccessible(true);
    final QueueingRecycler<?> contextRecycler = (QueueingRecycler<?>) field.get(layout);
    final MpmcArrayQueue<?> queue = (MpmcArrayQueue<?>) contextRecycler.getQueue();
    Assertions.assertThat(queue.capacity()).isEqualTo(512);
}
Also used : Field(java.lang.reflect.Field) MpmcArrayQueue(org.jctools.queues.MpmcArrayQueue) JsonTemplateLayout(org.apache.logging.log4j.layout.template.json.JsonTemplateLayout) Test(org.junit.jupiter.api.Test) LoggerContextSource(org.apache.logging.log4j.core.test.junit.LoggerContextSource)

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