Search in sources :

Example 11 with SimpleMessage

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

the class JsonTemplateLayoutTest method test_maxStringLength.

@Test
void test_maxStringLength() {
    // Create the log event.
    final int maxStringLength = 30;
    final String excessiveMessageString = Strings.repeat("m", maxStringLength) + 'M';
    final SimpleMessage message = new SimpleMessage(excessiveMessageString);
    final Throwable thrown = new RuntimeException();
    final LogEvent logEvent = Log4jLogEvent.newBuilder().setLoggerName(LOGGER_NAME).setLevel(Level.INFO).setMessage(message).setThrown(thrown).build();
    // Create the event template node with map values.
    final String messageKey = "message";
    final String excessiveKey = Strings.repeat("k", maxStringLength) + 'K';
    final String excessiveValue = Strings.repeat("v", maxStringLength) + 'V';
    final String nullValueKey = "nullValueKey";
    final String eventTemplate = writeJson(asMap(messageKey, asMap("$resolver", "message"), excessiveKey, excessiveValue, nullValueKey, asMap("$resolver", "exception", "field", "message")));
    // Create the layout.
    final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).setMaxStringLength(maxStringLength).build();
    // Check serialized event.
    usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
        final String truncatedStringSuffix = JsonTemplateLayoutDefaults.getTruncatedStringSuffix();
        final String truncatedMessageString = excessiveMessageString.substring(0, maxStringLength) + truncatedStringSuffix;
        assertThat(accessor.getString(messageKey)).isEqualTo(truncatedMessageString);
        final String truncatedKey = excessiveKey.substring(0, maxStringLength) + truncatedStringSuffix;
        final String truncatedValue = excessiveValue.substring(0, maxStringLength) + truncatedStringSuffix;
        assertThat(accessor.getString(truncatedKey)).isEqualTo(truncatedValue);
        assertThat(accessor.getString(nullValueKey)).isNull();
    });
}
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 12 with SimpleMessage

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

the class JsonTemplateLayoutTest method test_exception_resolvers_against_no_exceptions.

@Test
void test_exception_resolvers_against_no_exceptions() {
    // Create the log event.
    final SimpleMessage message = new SimpleMessage("Hello, World!");
    final LogEvent logEvent = Log4jLogEvent.newBuilder().setLoggerName(LOGGER_NAME).setMessage(message).build();
    // Create the event template.
    final String eventTemplate = writeJson(asMap("exStackTrace", asMap("$resolver", "exception", "field", "stackTrace"), "exStackTraceString", asMap("$resolver", "exception", "field", "stackTrace", "stackTrace", asMap("stringified", true)), "exRootCauseStackTrace", asMap("$resolver", "exceptionRootCause", "field", "stackTrace"), "exRootCauseStackTraceString", asMap("$resolver", "exceptionRootCause", "field", "stackTrace", "stackTrace", asMap("stringified", true)), "requiredFieldTriggeringError", true));
    // Create the layout.
    final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).setStackTraceEnabled(true).build();
    // Check the serialized event.
    usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
        assertThat(accessor.getObject("exStackTrace")).isNull();
        assertThat(accessor.getObject("exStackTraceString")).isNull();
        assertThat(accessor.getObject("exRootCauseStackTrace")).isNull();
        assertThat(accessor.getObject("exRootCauseStackTraceString")).isNull();
        assertThat(accessor.getBoolean("requiredFieldTriggeringError")).isTrue();
    });
}
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 13 with SimpleMessage

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

the class LogEventFixture method createFullLogEvent.

private static LogEvent createFullLogEvent(final String id, final long timeMillis) {
    // Create exception.
    final Exception sourceHelper = new Exception();
    sourceHelper.fillInStackTrace();
    final Exception cause = new NullPointerException("testNPEx-" + id);
    sourceHelper.fillInStackTrace();
    final StackTraceElement source = sourceHelper.getStackTrace()[0];
    final IOException ioException = new IOException("testIOEx-" + id, cause);
    ioException.addSuppressed(new IndexOutOfBoundsException("I am suppressed exception 1" + id));
    ioException.addSuppressed(new IndexOutOfBoundsException("I am suppressed exception 2" + id));
    // Create rest of the event attributes.
    final SimpleMessage message = new SimpleMessage("full LogEvent message " + id);
    final StringMap contextData = createContextData(id);
    final ThreadContextStack contextStack = createContextStack(id);
    final int threadId = id.hashCode();
    final String threadName = "MyThreadName" + id;
    final int threadPriority = threadId % 10;
    final Level level = Level.DEBUG;
    final String loggerFqcn = "f.q.c.n" + id;
    final String loggerName = "a.B" + id;
    final long nanoTime = timeMillis * 2;
    // Create the event.
    return Log4jLogEvent.newBuilder().setLoggerName(loggerName).setLoggerFqcn(loggerFqcn).setLevel(level).setMessage(message).setThrown(ioException).setContextData(contextData).setContextStack(contextStack).setThreadId(threadId).setThreadName(threadName).setThreadPriority(threadPriority).setSource(source).setTimeMillis(timeMillis).setNanoTime(nanoTime).build();
}
Also used : MutableThreadContextStack(org.apache.logging.log4j.spi.MutableThreadContextStack) ThreadContextStack(org.apache.logging.log4j.spi.ThreadContextStack) StringMap(org.apache.logging.log4j.util.StringMap) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Level(org.apache.logging.log4j.Level) IOException(java.io.IOException) IOException(java.io.IOException)

Example 14 with SimpleMessage

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

the class JsonTemplateLayoutTest method test_custom_resolver.

@Test
void test_custom_resolver() {
    // Create the event template.
    final String eventTemplate = writeJson(asMap("customField", asMap("$resolver", "custom")));
    // Create the layout.
    final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).build();
    // Create the log event.
    final SimpleMessage message = new SimpleMessage("foo");
    final LogEvent logEvent = Log4jLogEvent.newBuilder().setLoggerName(LOGGER_NAME).setMessage(message).build();
    // Check the serialized log event.
    usingSerializedLogEventAccessor(layout, logEvent, accessor -> Assertions.assertThat(accessor.getString("customField")).matches("CustomValue-[0-9]+"));
}
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 15 with SimpleMessage

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

the class JsonTemplateLayoutTest method test_property_injection.

@Test
void test_property_injection() {
    // Create the log event.
    final SimpleMessage message = new SimpleMessage("Hello, World");
    final LogEvent logEvent = Log4jLogEvent.newBuilder().setLoggerName(LOGGER_NAME).setLevel(Level.INFO).setMessage(message).build();
    // Create the event template with property.
    final String propertyName = "propertyName";
    final String eventTemplate = writeJson(asMap(propertyName, "${" + propertyName + "}"));
    // Create the layout with property.
    final String propertyValue = "propertyValue";
    final Configuration config = ConfigurationBuilderFactory.newConfigurationBuilder().addProperty(propertyName, propertyValue).build();
    final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(config).setEventTemplate(eventTemplate).build();
    // Check the serialized event.
    usingSerializedLogEventAccessor(layout, logEvent, accessor -> assertThat(accessor.getString(propertyName)).isEqualTo(propertyValue));
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) 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)

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