use of org.apache.logging.log4j.util.IndexedReadOnlyStringMap in project logging-log4j2 by apache.
the class MapPatternConverter method format.
/**
* {@inheritDoc}
*/
@Override
public void format(final LogEvent event, final StringBuilder toAppendTo) {
MapMessage msg;
if (event.getMessage() instanceof MapMessage) {
msg = (MapMessage) event.getMessage();
} else {
return;
}
final IndexedReadOnlyStringMap sortedMap = msg.getIndexedReadOnlyStringMap();
// Key/Value pair for the Map in a similar format to Hashtable.toString()
if (key == null) {
if (sortedMap.isEmpty()) {
toAppendTo.append("{}");
return;
}
toAppendTo.append("{");
for (int i = 0; i < sortedMap.size(); i++) {
if (i > 0) {
toAppendTo.append(", ");
}
toAppendTo.append(sortedMap.getKeyAt(i)).append('=').append((String) sortedMap.getValueAt(i));
}
toAppendTo.append('}');
} else {
// otherwise they just want a single key output
final String val = sortedMap.getValue(key);
if (val != null) {
toAppendTo.append(val);
}
}
}
use of org.apache.logging.log4j.util.IndexedReadOnlyStringMap in project logging-log4j2 by apache.
the class MapFilterTest method testConfig.
@Test
@LoggerContextSource("log4j2-mapfilter.xml")
public void testConfig(final Configuration config, @Named("LIST") final ListAppender app) {
final Filter filter = config.getFilter();
assertNotNull(filter, "No MapFilter");
assertTrue(filter instanceof MapFilter, "Not a MapFilter");
final MapFilter mapFilter = (MapFilter) filter;
assertFalse(mapFilter.isAnd(), "Should not be And filter");
final IndexedReadOnlyStringMap map = mapFilter.getStringMap();
assertNotNull(map, "No Map");
assertFalse(map.isEmpty(), "No elements in Map");
assertEquals(1, map.size(), "Incorrect number of elements in Map");
assertTrue(map.containsKey("eventId"), "Map does not contain key eventId");
assertEquals(2, map.<Collection<?>>getValue("eventId").size(), "List does not contain 2 elements");
final Logger logger = LogManager.getLogger(MapFilterTest.class);
final Map<String, String> eventMap = new HashMap<>();
eventMap.put("eventId", "Login");
logger.debug(new StringMapMessage(eventMap));
final List<String> msgs = app.getMessages();
assertNotNull(msgs, "No messages");
assertFalse(msgs.isEmpty(), "No messages");
}
use of org.apache.logging.log4j.util.IndexedReadOnlyStringMap in project logging-log4j2 by apache.
the class StructuredDataFilter method filter.
protected Result filter(final StructuredDataMessage message) {
boolean match = false;
final IndexedReadOnlyStringMap map = getStringMap();
for (int i = 0; i < map.size(); i++) {
final StringBuilder toMatch = getValue(message, map.getKeyAt(i));
if (toMatch != null) {
match = listContainsValue((List<String>) map.getValueAt(i), toMatch);
} else {
match = false;
}
if ((!isAnd() && match) || (isAnd() && !match)) {
break;
}
}
return match ? onMatch : onMismatch;
}
use of org.apache.logging.log4j.util.IndexedReadOnlyStringMap in project logging-log4j2 by apache.
the class ThreadContextMapFilter method filter.
private Result filter() {
boolean match = false;
if (useMap) {
ReadOnlyStringMap currentContextData = null;
final IndexedReadOnlyStringMap map = getStringMap();
for (int i = 0; i < map.size(); i++) {
if (currentContextData == null) {
currentContextData = currentContextData();
}
final String toMatch = currentContextData.getValue(map.getKeyAt(i));
match = toMatch != null && ((List<String>) map.getValueAt(i)).contains(toMatch);
if ((!isAnd() && match) || (isAnd() && !match)) {
break;
}
}
} else {
match = value.equals(currentContextData().getValue(key));
}
return match ? onMatch : onMismatch;
}
use of org.apache.logging.log4j.util.IndexedReadOnlyStringMap in project logging-log4j2 by apache.
the class StructuredDataFilterTest method testConfig.
@Test
@LoggerContextSource("log4j2-sdfilter.xml")
public void testConfig(final Configuration config) {
final Filter filter = config.getFilter();
assertNotNull(filter, "No StructuredDataFilter");
assertTrue(filter instanceof StructuredDataFilter, "Not a StructuredDataFilter");
final StructuredDataFilter sdFilter = (StructuredDataFilter) filter;
assertFalse(sdFilter.isAnd(), "Should not be And filter");
final IndexedReadOnlyStringMap map = sdFilter.getStringMap();
assertNotNull(map, "No Map");
assertFalse(map.isEmpty(), "No elements in Map");
assertEquals(1, map.size(), "Incorrect number of elements in Map");
assertTrue(map.containsKey("eventId"), "Map does not contain key eventId");
assertEquals(2, map.<Collection<?>>getValue("eventId").size(), "List does not contain 2 elements");
}
Aggregations