Search in sources :

Example 1 with JsonTemplateLayout

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

the class CounterResolverTest method verify.

private static void verify(final String eventTemplate, final Object expectedNumber1, final Object expectedNumber2) {
    // Create the layout.
    final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).build();
    // Create the log event.
    final LogEvent logEvent = Log4jLogEvent.newBuilder().build();
    // Check the 1st serialized event.
    final String serializedLogEvent1 = layout.toSerializable(logEvent);
    final Object deserializedLogEvent1 = JsonReader.read(serializedLogEvent1);
    assertThat(deserializedLogEvent1).isEqualTo(expectedNumber1);
    // Check the 2nd serialized event.
    final String serializedLogEvent2 = layout.toSerializable(logEvent);
    final Object deserializedLogEvent2 = JsonReader.read(serializedLogEvent2);
    assertThat(deserializedLogEvent2).isEqualTo(expectedNumber2);
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) JsonTemplateLayout(org.apache.logging.log4j.layout.template.json.JsonTemplateLayout)

Example 2 with JsonTemplateLayout

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

the class MessageResolverTest method test_message_fallbackKey.

@Test
void test_message_fallbackKey() {
    // Create the event template.
    final String eventTemplate = writeJson(asMap("message", asMap("$resolver", "message", "fallbackKey", "formattedMessage")));
    // Create the layout.
    final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).build();
    // Create a log event with a MapMessage.
    final Message mapMessage = new StringMapMessage().with("key1", "val1");
    final LogEvent mapMessageLogEvent = Log4jLogEvent.newBuilder().setMessage(mapMessage).setTimeMillis(System.currentTimeMillis()).build();
    // Check the serialized MapMessage.
    usingSerializedLogEventAccessor(layout, mapMessageLogEvent, accessor -> assertThat(accessor.getString(new String[] { "message", "key1" })).isEqualTo("val1"));
    // Create a log event with a SimpleMessage.
    final Message simpleMessage = new SimpleMessage("simple");
    final LogEvent simpleMessageLogEvent = Log4jLogEvent.newBuilder().setMessage(simpleMessage).setTimeMillis(System.currentTimeMillis()).build();
    // Check the serialized MapMessage.
    usingSerializedLogEventAccessor(layout, simpleMessageLogEvent, accessor -> assertThat(accessor.getString(new String[] { "message", "formattedMessage" })).isEqualTo("simple"));
}
Also used : StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) 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) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) JsonTemplateLayout(org.apache.logging.log4j.layout.template.json.JsonTemplateLayout) Test(org.junit.jupiter.api.Test)

Example 3 with JsonTemplateLayout

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

the class MessageResolverTest method test_ObjectMessage.

@Test
void test_ObjectMessage() {
    // Create the log event.
    final int id = 0xDEADBEEF;
    final String name = "name-" + id;
    final Object attachment = new LinkedHashMap<String, Object>() {

        {
            put("id", id);
            put("name", name);
        }
    };
    final ObjectMessage message = new ObjectMessage(attachment);
    final LogEvent logEvent = Log4jLogEvent.newBuilder().setMessage(message).build();
    // Create the event template.
    final String eventTemplate = writeJson(asMap("message", asMap("$resolver", "message")));
    // Create the layout.
    JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setStackTraceEnabled(true).setEventTemplate(eventTemplate).build();
    // Check the serialized event.
    usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
        assertThat(accessor.getInteger(new String[] { "message", "id" })).isEqualTo(id);
        assertThat(accessor.getString(new String[] { "message", "name" })).isEqualTo(name);
    });
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) JsonTemplateLayout(org.apache.logging.log4j.layout.template.json.JsonTemplateLayout) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.jupiter.api.Test)

Example 4 with JsonTemplateLayout

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

the class CaseConverterResolverTest method test_errorHandlingStrategy.

@ParameterizedTest
@CsvSource({ // failure message              | locale    | input     | strategy      | replacement   | output
"" + ",nl" + ",1" + ",pass" + ",null" + ",1", "" + ",nl" + ",[2]" + ",pass" + ",null" + ",[2]", "was expecting a string value" + ",nl" + ",1" + ",fail" + ",null" + ",null", "" + ",nl" + ",1" + ",replace" + ",null" + ",null", "" + ",nl" + ",1" + ",replace" + ",2" + ",2", "" + ",nl" + ",1" + ",replace" + ",\"s\"" + ",\"s\"" })
void test_errorHandlingStrategy(final String failureMessage, final String locale, final String inputJson, final String errorHandlingStrategy, final String replacementJson, final String outputJson) {
    // Parse arguments.
    final Object input = readJson(inputJson);
    final Object replacement = readJson(replacementJson);
    final Object output = readJson(outputJson);
    // Create the event template.
    final String eventTemplate = writeJson(asMap("output", asMap("$resolver", "caseConverter", "case", "lower", "locale", locale, "input", input, "errorHandlingStrategy", errorHandlingStrategy, "replacement", replacement)));
    // Create the layout.
    final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).build();
    // Create the log event.
    final LogEvent logEvent = Log4jLogEvent.newBuilder().build();
    // Check the serialized event.
    final boolean failureExpected = Strings.isNotBlank(failureMessage);
    if (failureExpected) {
        assertThatThrownBy(() -> layout.toSerializable(logEvent)).hasMessageContaining(failureMessage);
    } else {
        usingSerializedLogEventAccessor(layout, logEvent, accessor -> assertThat(accessor.getObject("output")).isEqualTo(output));
    }
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) JsonTemplateLayout(org.apache.logging.log4j.layout.template.json.JsonTemplateLayout) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with JsonTemplateLayout

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

the class CaseConverterResolverTest method test_upper.

@ParameterizedTest
@CsvSource({ // case     | locale    | input         | output
"upper" + ",nl" + ",ioz" + ",IOZ", "upper" + ",nl" + ",IOZ" + ",IOZ", "lower" + ",nl" + ",ioz" + ",ioz", "lower" + ",nl" + ",IOZ" + ",ioz", "upper" + ",tr" + ",ıiğüşöç" + ",IİĞÜŞÖÇ", "upper" + ",tr" + ",IİĞÜŞÖÇ" + ",IİĞÜŞÖÇ", "lower" + ",tr" + ",ıiğüşöç" + ",ıiğüşöç", "lower" + ",tr" + ",IİĞÜŞÖÇ" + ",ıiğüşöç" })
void test_upper(final String case_, final String locale, final String input, final String output) {
    // Create the event template.
    final String eventTemplate = writeJson(asMap("output", asMap("$resolver", "caseConverter", "case", case_, "locale", locale, "input", asMap("$resolver", "mdc", "key", "input"))));
    // Create the layout.
    final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).build();
    // Create the log event.
    final StringMap contextData = new SortedArrayStringMap();
    contextData.putValue("input", input);
    final LogEvent logEvent = Log4jLogEvent.newBuilder().setContextData(contextData).build();
    // Check the serialized event.
    usingSerializedLogEventAccessor(layout, logEvent, accessor -> assertThat(accessor.getString("output")).isEqualTo(output));
}
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) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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