Search in sources :

Example 16 with StringMapMessage

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

the class InterpolatorTest method testInterpolatorMapMessageWithNoPrefixConfigDoesntMatch.

@Test
public void testInterpolatorMapMessageWithNoPrefixConfigDoesntMatch() {
    Interpolator interpolator = new Interpolator(Collections.emptyMap());
    final HashMap<String, String> map = new HashMap<>();
    map.put("key", "mapMessage");
    LogEvent event = Log4jLogEvent.newBuilder().setLoggerName(getClass().getName()).setLoggerFqcn(Logger.class.getName()).setLevel(Level.INFO).setMessage(new StringMapMessage(map)).build();
    assertNull(interpolator.lookup(event, "key"), "Values without a map prefix should not match MapMessages");
}
Also used : StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) HashMap(java.util.HashMap) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) Test(org.junit.jupiter.api.Test)

Example 17 with StringMapMessage

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

the class MapMessageLookupTest method testStringMapMessageLookup.

@Test
public void testStringMapMessageLookup() {
    // GIVEN: A StringMapMessage object
    final Map<String, String> values = new HashMap<>(3);
    values.put("A", "a");
    values.put("B", "b");
    values.put("C", "c");
    final MapMessage message = new StringMapMessage(values);
    // AND: An event with that message
    final LogEvent event = Log4jLogEvent.newBuilder().setLevel(Level.DEBUG).setMessage(message).build();
    // AND: A MapMessageLookup object
    final MapMessageLookup lookup = new MapMessageLookup();
    // WHEN: Lookup is performed
    final String a = lookup.lookup(event, "A");
    final String b = lookup.lookup(event, "B");
    final String c = lookup.lookup(event, "C");
    // THEN: The looked up values are correct
    assertEquals("a", a);
    assertEquals("b", b);
    assertEquals("c", c);
}
Also used : StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) MapMessageLookup(org.apache.logging.log4j.lookup.MapMessageLookup) HashMap(java.util.HashMap) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) MapMessage(org.apache.logging.log4j.message.MapMessage) StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) Test(org.junit.Test)

Example 18 with StringMapMessage

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

the class MessagePatternConverterTest method testMapMessageFormatJson.

@Test
public void testMapMessageFormatJson() {
    final MessagePatternConverter converter = MessagePatternConverter.newInstance(null, new String[] { "json" });
    Message msg = new StringMapMessage().with("key", "val");
    LogEvent event = // 
    Log4jLogEvent.newBuilder().setLoggerName(// 
    "MyLogger").setLevel(// 
    Level.DEBUG).setMessage(msg).build();
    StringBuilder sb = new StringBuilder();
    converter.format(event, sb);
    assertEquals("{\"key\":\"val\"}", sb.toString(), "Unexpected result");
}
Also used : StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) 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 19 with StringMapMessage

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

the class MessageResolverTest method test_StringMapMessage.

@Test
void test_StringMapMessage() {
    // Create the log event.
    final StringMapMessage message = new StringMapMessage();
    message.put("message", "Hello, World!");
    message.put("bottle", "Kickapoo Joy Juice");
    final LogEvent logEvent = Log4jLogEvent.newBuilder().setMessage(message).build();
    // Create the event template.
    final String eventTemplate = writeJson(asMap("message", asMap("$resolver", "message")));
    // 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(new String[] { "message", "message" })).isEqualTo("Hello, World!");
        assertThat(accessor.getString(new String[] { "message", "bottle" })).isEqualTo("Kickapoo Joy Juice");
    });
}
Also used : StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) JsonTemplateLayout(org.apache.logging.log4j.layout.template.json.JsonTemplateLayout) Test(org.junit.jupiter.api.Test)

Example 20 with StringMapMessage

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

the class MessageResolverTest method test_MapMessage_serialization.

@Test
void test_MapMessage_serialization() {
    // Create the event template.
    final String eventTemplate = writeJson(asMap("message", asMap("$resolver", "message")));
    // Create the layout.
    final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).build();
    // Create the log event with a MapMessage.
    final StringMapMessage mapMessage = new StringMapMessage().with("key1", "val1").with("key2", 0xDEADBEEF).with("key3", Collections.singletonMap("key3.1", "val3.1"));
    final LogEvent logEvent = Log4jLogEvent.newBuilder().setMessage(mapMessage).setTimeMillis(System.currentTimeMillis()).build();
    // Check the serialized event.
    usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
        assertThat(accessor.getString(new String[] { "message", "key1" })).isEqualTo("val1");
        assertThat(accessor.getInteger(new String[] { "message", "key2" })).isEqualTo(0xDEADBEEF);
        assertThat(accessor.getString(new String[] { "message", "key3", "key3.1" })).isEqualTo("val3.1");
    });
}
Also used : StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) JsonTemplateLayout(org.apache.logging.log4j.layout.template.json.JsonTemplateLayout) Test(org.junit.jupiter.api.Test)

Aggregations

StringMapMessage (org.apache.logging.log4j.message.StringMapMessage)25 Test (org.junit.jupiter.api.Test)23 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)21 LogEvent (org.apache.logging.log4j.core.LogEvent)20 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)8 HashMap (java.util.HashMap)7 Message (org.apache.logging.log4j.message.Message)7 JsonTemplateLayout (org.apache.logging.log4j.layout.template.json.JsonTemplateLayout)5 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)4 Logger (org.apache.logging.log4j.Logger)3 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)3 KeyValuePair (org.apache.logging.log4j.core.util.KeyValuePair)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Collection (java.util.Collection)1 Filter (org.apache.logging.log4j.core.Filter)1 LoggerContextSource (org.apache.logging.log4j.core.test.junit.LoggerContextSource)1 MapMessageLookup (org.apache.logging.log4j.lookup.MapMessageLookup)1 MapMessage (org.apache.logging.log4j.message.MapMessage)1 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)1