Search in sources :

Example 11 with MapMessage

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

the class MapPatternConverterTest method testConverter.

@Test
public void testConverter() {
    final MapMessage msg = new MapMessage();
    msg.put("subject", "I");
    msg.put("verb", "love");
    msg.put("object", "Log4j");
    final MapPatternConverter converter = MapPatternConverter.newInstance(null);
    final LogEvent event = //
    Log4jLogEvent.newBuilder().setLoggerName(//
    "MyLogger").setLevel(//
    Level.DEBUG).setMessage(//
    msg).build();
    final StringBuilder sb = new StringBuilder();
    converter.format(event, sb);
    final String str = sb.toString();
    String expected = "subject=I";
    assertTrue("Missing or incorrect subject. Expected " + expected + ", actual " + str, str.contains(expected));
    expected = "verb=love";
    assertTrue("Missing or incorrect verb", str.contains(expected));
    expected = "object=Log4j";
    assertTrue("Missing or incorrect object", str.contains(expected));
    assertEquals("{object=Log4j, subject=I, verb=love}", str);
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) MapMessage(org.apache.logging.log4j.message.MapMessage) Test(org.junit.Test)

Example 12 with MapMessage

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

the class CollectionLoggingTest method testSimpleMap.

@Test
public void testSimpleMap() {
    final Logger logger = context.getLogger(CollectionLoggingTest.class.getName());
    logger.error(System.getProperties());
    final Map<String, String> map = new HashMap<>();
    map.put("MyKey1", "MyValue1");
    map.put("MyKey2", "MyValue2");
    logger.error(new MapMessage(map));
    logger.error(map);
// TODO: some assertions
}
Also used : HashMap(java.util.HashMap) MapMessage(org.apache.logging.log4j.message.MapMessage) Logger(org.apache.logging.log4j.Logger) Test(org.junit.Test)

Example 13 with MapMessage

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

the class MapFilterTest method testConfig.

@Test
public void testConfig() {
    final Configuration config = context.getConfiguration();
    final Filter filter = config.getFilter();
    assertNotNull("No MapFilter", filter);
    assertTrue("Not a MapFilter", filter instanceof MapFilter);
    final MapFilter mapFilter = (MapFilter) filter;
    assertFalse("Should not be And filter", mapFilter.isAnd());
    final Map<String, List<String>> map = mapFilter.getMap();
    assertNotNull("No Map", map);
    assertFalse("No elements in Map", map.isEmpty());
    assertEquals("Incorrect number of elements in Map", 1, map.size());
    assertTrue("Map does not contain key eventId", map.containsKey("eventId"));
    assertEquals("List does not contain 2 elements", 2, map.get("eventId").size());
    final Logger logger = LogManager.getLogger(MapFilterTest.class);
    final Map<String, String> eventMap = new HashMap<>();
    eventMap.put("eventId", "Login");
    logger.debug(new MapMessage(eventMap));
    final ListAppender app = context.getListAppender("LIST");
    final List<String> msgs = app.getMessages();
    assertNotNull("No messages", msgs);
    assertFalse("No messages", msgs.isEmpty());
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) Filter(org.apache.logging.log4j.core.Filter) HashMap(java.util.HashMap) MapMessage(org.apache.logging.log4j.message.MapMessage) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Test(org.junit.Test)

Aggregations

MapMessage (org.apache.logging.log4j.message.MapMessage)13 Test (org.junit.Test)8 LogEvent (org.apache.logging.log4j.core.LogEvent)6 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)6 HashMap (java.util.HashMap)5 Message (org.apache.logging.log4j.message.Message)3 Logger (org.apache.logging.log4j.Logger)2 KeyValuePair (org.apache.logging.log4j.core.util.KeyValuePair)2 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)2 Sampler (com.google.monitoring.runtime.instrumentation.Sampler)1 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 MessageConsumer (javax.jms.MessageConsumer)1 Marker (org.apache.logging.log4j.Marker)1 Filter (org.apache.logging.log4j.core.Filter)1 Configuration (org.apache.logging.log4j.core.config.Configuration)1 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)1