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);
}
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
}
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());
}
Aggregations