Search in sources :

Example 16 with SimpleMessage

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

the class JsonTemplateLayoutTest method test_PatternResolver.

@Test
void test_PatternResolver() {
    // Create the event template.
    final String eventTemplate = writeJson(asMap("message", asMap("$resolver", "pattern", "pattern", "%p:%m")));
    // Create the layout.
    final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).build();
    // Create the log event.
    final SimpleMessage message = new SimpleMessage("foo");
    final Level level = Level.FATAL;
    final LogEvent logEvent = Log4jLogEvent.newBuilder().setLoggerName(LOGGER_NAME).setMessage(message).setLevel(level).build();
    // Check the serialized event.
    usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
        final String expectedMessage = String.format("%s:%s", level, message.getFormattedMessage());
        assertThat(accessor.getString("message")).isEqualTo(expectedMessage);
    });
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Level(org.apache.logging.log4j.Level) Test(org.junit.jupiter.api.Test)

Example 17 with SimpleMessage

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

the class JsonTemplateLayoutTest method test_stringified_exception_resolver_with_maxStringLength.

@Test
void test_stringified_exception_resolver_with_maxStringLength() {
    // Create the event template.
    final String eventTemplate = writeJson(asMap("stackTrace", asMap("$resolver", "exception", "field", "stackTrace", "stringified", true)));
    // Create the layout.
    final int maxStringLength = eventTemplate.length();
    final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).setMaxStringLength(maxStringLength).setStackTraceEnabled(true).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.
    usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
        final int expectedLength = maxStringLength + JsonTemplateLayoutDefaults.getTruncatedStringSuffix().length();
        assertThat(accessor.getString("stackTrace").length()).isEqualTo(expectedLength);
    });
}
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 18 with SimpleMessage

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

the class JsonTemplateLayoutTest method test_event_template_additional_fields.

@Test
void test_event_template_additional_fields() {
    // Create the log event.
    final SimpleMessage message = new SimpleMessage("Hello, World!");
    final RuntimeException exception = NonAsciiUtf8MethodNameContainingException.INSTANCE;
    final Level level = Level.ERROR;
    final LogEvent logEvent = Log4jLogEvent.newBuilder().setLoggerName(LOGGER_NAME).setLevel(level).setMessage(message).setThrown(exception).build();
    // Create the event template.
    final String eventTemplate = "{}";
    // Create the layout.
    final EventTemplateAdditionalField[] additionalFields = { EventTemplateAdditionalField.newBuilder().setKey("number").setValue("1").setFormat(EventTemplateAdditionalField.Format.JSON).build(), EventTemplateAdditionalField.newBuilder().setKey("string").setValue("foo").build(), EventTemplateAdditionalField.newBuilder().setKey("level").setValue("{\"$resolver\": \"level\", \"field\": \"name\"}").setFormat(EventTemplateAdditionalField.Format.JSON).build() };
    final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setStackTraceEnabled(true).setEventTemplate(eventTemplate).setEventTemplateAdditionalFields(additionalFields).build();
    // Check the serialized event.
    usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
        assertThat(accessor.getInteger("number")).isEqualTo(1);
        assertThat(accessor.getString("string")).isEqualTo("foo");
        assertThat(accessor.getString("level")).isEqualTo(level.name());
    });
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Level(org.apache.logging.log4j.Level) EventTemplateAdditionalField(org.apache.logging.log4j.layout.template.json.JsonTemplateLayout.EventTemplateAdditionalField) Test(org.junit.jupiter.api.Test)

Example 19 with SimpleMessage

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

the class JsonTemplateLayoutTest method test_root_cause.

@Test
void test_root_cause() {
    // Create the log event.
    final SimpleMessage message = new SimpleMessage("Hello, World!");
    final RuntimeException exceptionCause = new RuntimeException("failure cause for test purposes");
    final RuntimeException exception = new RuntimeException("failure for test purposes", exceptionCause);
    final LogEvent logEvent = Log4jLogEvent.newBuilder().setLoggerName(LOGGER_NAME).setLevel(Level.ERROR).setMessage(message).setThrown(exception).build();
    // Create the event template.
    final String eventTemplate = writeJson(asMap("ex_class", asMap("$resolver", "exception", "field", "className"), "ex_message", asMap("$resolver", "exception", "field", "message"), "ex_stacktrace", asMap("$resolver", "exception", "field", "stackTrace", "stringified", true), "root_ex_class", asMap("$resolver", "exceptionRootCause", "field", "className"), "root_ex_message", asMap("$resolver", "exceptionRootCause", "field", "message"), "root_ex_stacktrace", asMap("$resolver", "exceptionRootCause", "field", "stackTrace", "stackTrace", asMap("stringified", true))));
    // 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("ex_class")).isEqualTo(exception.getClass().getCanonicalName());
        assertThat(accessor.getString("ex_message")).isEqualTo(exception.getMessage());
        assertThat(accessor.getString("ex_stacktrace")).startsWith(exception.getClass().getCanonicalName() + ": " + exception.getMessage());
        assertThat(accessor.getString("root_ex_class")).isEqualTo(exceptionCause.getClass().getCanonicalName());
        assertThat(accessor.getString("root_ex_message")).isEqualTo(exceptionCause.getMessage());
        assertThat(accessor.getString("root_ex_stacktrace")).startsWith(exceptionCause.getClass().getCanonicalName() + ": " + exceptionCause.getMessage());
    });
}
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 20 with SimpleMessage

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

the class JsonTemplateLayoutTest method test_StackTraceElement_template.

@Test
void test_StackTraceElement_template() {
    // Create the stack trace element template.
    final String classNameFieldName = "className";
    final String methodNameFieldName = "methodName";
    final String fileNameFieldName = "fileName";
    final String lineNumberFieldName = "lineNumber";
    final String stackTraceElementTemplate = writeJson(asMap(classNameFieldName, asMap("$resolver", "stackTraceElement", "field", "className"), methodNameFieldName, asMap("$resolver", "stackTraceElement", "field", "methodName"), fileNameFieldName, asMap("$resolver", "stackTraceElement", "field", "fileName"), lineNumberFieldName, asMap("$resolver", "stackTraceElement", "field", "lineNumber")));
    // Create the event template.
    final String stackTraceFieldName = "stackTrace";
    final String eventTemplate = writeJson(asMap(stackTraceFieldName, asMap("$resolver", "exception", "field", "stackTrace")));
    // Create the layout.
    final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setStackTraceEnabled(true).setStackTraceElementTemplate(stackTraceElementTemplate).setEventTemplate(eventTemplate).build();
    // Create the log event.
    final SimpleMessage message = new SimpleMessage("Hello, World!");
    final RuntimeException exceptionCause = new RuntimeException("failure cause for test purposes");
    final RuntimeException exception = new RuntimeException("failure for test purposes", exceptionCause);
    final LogEvent logEvent = Log4jLogEvent.newBuilder().setLoggerName(LOGGER_NAME).setLevel(Level.ERROR).setMessage(message).setThrown(exception).build();
    // Check the serialized event.
    usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
        assertThat(accessor.exists(stackTraceFieldName)).isTrue();
        @SuppressWarnings("unchecked") final List<Map<String, Object>> deserializedStackTraceElements = accessor.getObject(stackTraceFieldName, List.class);
        final StackTraceElement[] stackTraceElements = exception.getStackTrace();
        assertThat(deserializedStackTraceElements.size()).isEqualTo(stackTraceElements.length);
        for (int stackTraceElementIndex = 0; stackTraceElementIndex < stackTraceElements.length; stackTraceElementIndex++) {
            final StackTraceElement stackTraceElement = stackTraceElements[stackTraceElementIndex];
            final Map<String, Object> deserializedStackTraceElement = deserializedStackTraceElements.get(stackTraceElementIndex);
            assertThat(deserializedStackTraceElement.size()).isEqualTo(4);
            assertThat(deserializedStackTraceElement.get(classNameFieldName)).isEqualTo(stackTraceElement.getClassName());
            assertThat(deserializedStackTraceElement.get(methodNameFieldName)).isEqualTo(stackTraceElement.getMethodName());
            assertThat(deserializedStackTraceElement.get(fileNameFieldName)).isEqualTo(stackTraceElement.getFileName());
            assertThat(deserializedStackTraceElement.get(lineNumberFieldName)).isEqualTo(stackTraceElement.getLineNumber());
        }
    });
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Map(java.util.Map) StringMap(org.apache.logging.log4j.util.StringMap) 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