Search in sources :

Example 11 with Message

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

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

the class ReadOnlyStringMapResolverTest method test_map_key_access.

@Test
public void test_map_key_access() {
    // Create the log event.
    final String directlyAccessedKey = "mapKey1";
    final String directlyAccessedValue = "mapValue1";
    final String directlyAccessedNullPropertyKey = "mapKey2";
    final Message message = new StringMapMessage().with(directlyAccessedKey, directlyAccessedValue);
    final LogEvent logEvent = Log4jLogEvent.newBuilder().setMessage(message).build();
    // Check the serialized event.
    testReadOnlyStringMapKeyAccess(directlyAccessedKey, directlyAccessedValue, directlyAccessedNullPropertyKey, logEvent, "map");
}
Also used : StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) 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) Test(org.junit.jupiter.api.Test)

Example 13 with Message

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

the class AbstractLogger method logMessage.

protected void logMessage(final String fqcn, final Level level, final Marker marker, final String message, final Supplier<?>... paramSuppliers) {
    final Message msg = messageFactory.newMessage(message, LambdaUtil.getAll(paramSuppliers));
    logMessageSafely(fqcn, level, marker, msg, msg.getThrowable());
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Message(org.apache.logging.log4j.message.Message) EntryMessage(org.apache.logging.log4j.message.EntryMessage) StringFormattedMessage(org.apache.logging.log4j.message.StringFormattedMessage)

Example 14 with Message

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

the class AbstractLogger method logMessage.

protected void logMessage(final String fqcn, final Level level, final Marker marker, final String message) {
    final Message msg = messageFactory.newMessage(message);
    logMessageSafely(fqcn, level, marker, msg, msg.getThrowable());
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Message(org.apache.logging.log4j.message.Message) EntryMessage(org.apache.logging.log4j.message.EntryMessage) StringFormattedMessage(org.apache.logging.log4j.message.StringFormattedMessage)

Example 15 with Message

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

the class AbstractLogger method logMessage.

protected void logMessage(final String fqcn, final Level level, final Marker marker, final MessageSupplier msgSupplier, final Throwable t) {
    final Message message = LambdaUtil.get(msgSupplier);
    logMessageSafely(fqcn, level, marker, message, (t == null && message != null) ? message.getThrowable() : t);
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Message(org.apache.logging.log4j.message.Message) EntryMessage(org.apache.logging.log4j.message.EntryMessage) StringFormattedMessage(org.apache.logging.log4j.message.StringFormattedMessage)

Aggregations

Message (org.apache.logging.log4j.message.Message)125 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)86 LogEvent (org.apache.logging.log4j.core.LogEvent)54 Test (org.junit.jupiter.api.Test)50 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)45 Marker (org.apache.logging.log4j.Marker)26 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)26 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)24 Level (org.apache.logging.log4j.Level)23 Test (org.junit.Test)22 StringMapMessage (org.apache.logging.log4j.message.StringMapMessage)19 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)18 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)17 MapMessage (org.apache.logging.log4j.message.MapMessage)15 StringMap (org.apache.logging.log4j.util.StringMap)15 DummyNanoClock (org.apache.logging.log4j.core.time.internal.DummyNanoClock)9 EntryMessage (org.apache.logging.log4j.message.EntryMessage)9 Map (java.util.Map)8 StringFormattedMessage (org.apache.logging.log4j.message.StringFormattedMessage)8 IOException (java.io.IOException)7