Search in sources :

Example 21 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.

the class JsonTemplateLayoutTest method test_unresolvable_nested_fields_are_skipped.

@Test
void test_unresolvable_nested_fields_are_skipped() {
    // Create the event template.
    final String eventTemplate = writeJson(asMap("exception", asMap("message", asMap("$resolver", "exception", "field", "message"), "className", asMap("$resolver", "exception", "field", "className")), "exceptionRootCause", asMap("message", asMap("$resolver", "exceptionRootCause", "field", "message"), "className", asMap("$resolver", "exceptionRootCause", "field", "className")), "source", asMap("lineNumber", asMap("$resolver", "source", "field", "lineNumber"), "fileName", asMap("$resolver", "source", "field", "fileName")), "emptyMap", Collections.emptyMap(), "emptyList", Collections.emptyList(), "null", null));
    // Create the layout.
    final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).setStackTraceEnabled(// Disable "exception" and "exceptionRootCause" resolvers.
    false).setLocationInfoEnabled(// Disable the "source" resolver.
    false).build();
    // Create the log event.
    final SimpleMessage message = new SimpleMessage("foo");
    final Exception thrown = new RuntimeException("bar");
    final LogEvent logEvent = Log4jLogEvent.newBuilder().setLoggerName(LOGGER_NAME).setMessage(message).setThrown(thrown).build();
    // Check the serialized event.
    final String expectedSerializedLogEventJson = "{}" + JsonTemplateLayoutDefaults.getEventDelimiter();
    final String actualSerializedLogEventJson = layout.toSerializable(logEvent);
    Assertions.assertThat(actualSerializedLogEventJson).isEqualTo(expectedSerializedLogEventJson);
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) EOFException(java.io.EOFException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 22 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.

the class JsonTemplateLayoutTest method test_marker_name.

@Test
void test_marker_name() {
    // Create the log event.
    final SimpleMessage message = new SimpleMessage("Hello, World!");
    final String markerName = "test";
    final Marker marker = MarkerManager.getMarker(markerName);
    final LogEvent logEvent = Log4jLogEvent.newBuilder().setLoggerName(LOGGER_NAME).setLevel(Level.ERROR).setMessage(message).setMarker(marker).build();
    // Create the event template.
    final String messageKey = "message";
    final String markerNameKey = "marker";
    final String eventTemplate = writeJson(asMap("message", asMap("$resolver", "message"), "marker", asMap("$resolver", "marker", "field", "name")));
    // Create the layout.
    final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).build();
    // Check the serialized event.
    usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
        assertThat(accessor.getString(messageKey)).isEqualTo(message.getFormattedMessage());
        assertThat(accessor.getString(markerNameKey)).isEqualTo(markerName);
    });
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Marker(org.apache.logging.log4j.Marker) Test(org.junit.jupiter.api.Test)

Example 23 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.

the class JsonTemplateLayoutTest method test_null_eventDelimiter.

@Test
void test_null_eventDelimiter() {
    // Create the event template.
    final String eventTemplate = writeJson(asMap("key", "val"));
    // Create the layout.
    final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).setEventDelimiter("\0").build();
    // Create the log event.
    final SimpleMessage message = new SimpleMessage("foo");
    final LogEvent logEvent = Log4jLogEvent.newBuilder().setLoggerName(LOGGER_NAME).setMessage(message).setThrown(NonAsciiUtf8MethodNameContainingException.INSTANCE).build();
    // Check the serialized event.
    final String serializedLogEvent = layout.toSerializable(logEvent);
    assertThat(serializedLogEvent).isEqualTo(eventTemplate + '\0');
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.jupiter.api.Test)

Example 24 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage 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 25 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.

the class ReadOnlyStringMapResolverTest method test_mdc_flatten.

@Test
void test_mdc_flatten() {
    // Create the log event.
    final SimpleMessage message = new SimpleMessage("Hello, World!");
    final StringMap contextData = new SortedArrayStringMap();
    final String mdcPatternMatchedKey = "mdcKey1";
    final String mdcPatternMatchedValue = "mdcValue1";
    contextData.putValue(mdcPatternMatchedKey, mdcPatternMatchedValue);
    final String mdcPatternMismatchedKey = "mdcKey2";
    final String mdcPatternMismatchedValue = "mdcValue2";
    contextData.putValue(mdcPatternMismatchedKey, mdcPatternMismatchedValue);
    final LogEvent logEvent = Log4jLogEvent.newBuilder().setMessage(message).setContextData(contextData).build();
    // Check the serialized event.
    testReadOnlyStringMapFlatten(mdcPatternMatchedKey, mdcPatternMatchedValue, mdcPatternMismatchedKey, logEvent, "mdc");
}
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) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) Test(org.junit.jupiter.api.Test)

Aggregations

SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)198 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)148 LogEvent (org.apache.logging.log4j.core.LogEvent)142 Test (org.junit.jupiter.api.Test)128 Message (org.apache.logging.log4j.message.Message)43 Marker (org.apache.logging.log4j.Marker)23 Test (org.junit.Test)21 StringMap (org.apache.logging.log4j.util.StringMap)20 Level (org.apache.logging.log4j.Level)17 IOException (java.io.IOException)13 SortedArrayStringMap (org.apache.logging.log4j.util.SortedArrayStringMap)12 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)11 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)10 LoggerContext (org.apache.logging.log4j.core.LoggerContext)10 ClockFactoryTest (org.apache.logging.log4j.core.time.ClockFactoryTest)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 ReusableMessage (org.apache.logging.log4j.message.ReusableMessage)5 ReusableObjectMessage (org.apache.logging.log4j.message.ReusableObjectMessage)5 StringMapMessage (org.apache.logging.log4j.message.StringMapMessage)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4