Search in sources :

Example 1 with MapMessageLookup

use of org.apache.logging.log4j.lookup.MapMessageLookup in project logging-log4j2 by apache.

the class MapMessageLookupTest method testStructuredDataMessageLookup.

@Test
public void testStructuredDataMessageLookup() {
    // GIVEN: A StructuredDataMessage object
    final StructuredDataMessage message = new StructuredDataMessage("id", "msg", "type");
    message.put("A", "a");
    message.put("B", "b");
    message.put("C", "c");
    // 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 : StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) MapMessageLookup(org.apache.logging.log4j.lookup.MapMessageLookup) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) Test(org.junit.Test)

Example 2 with MapMessageLookup

use of org.apache.logging.log4j.lookup.MapMessageLookup 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)

Aggregations

LogEvent (org.apache.logging.log4j.core.LogEvent)2 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)2 MapMessageLookup (org.apache.logging.log4j.lookup.MapMessageLookup)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 MapMessage (org.apache.logging.log4j.message.MapMessage)1 StringMapMessage (org.apache.logging.log4j.message.StringMapMessage)1 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)1