Search in sources :

Example 1 with IndexedReadOnlyStringMap

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);
        }
    }
}
Also used : MapMessage(org.apache.logging.log4j.message.MapMessage) IndexedReadOnlyStringMap(org.apache.logging.log4j.util.IndexedReadOnlyStringMap)

Example 2 with IndexedReadOnlyStringMap

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");
}
Also used : StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) Filter(org.apache.logging.log4j.core.Filter) HashMap(java.util.HashMap) Collection(java.util.Collection) IndexedReadOnlyStringMap(org.apache.logging.log4j.util.IndexedReadOnlyStringMap) Logger(org.apache.logging.log4j.Logger) Test(org.junit.jupiter.api.Test) LoggerContextSource(org.apache.logging.log4j.core.test.junit.LoggerContextSource)

Example 3 with IndexedReadOnlyStringMap

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;
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) IndexedReadOnlyStringMap(org.apache.logging.log4j.util.IndexedReadOnlyStringMap)

Example 4 with IndexedReadOnlyStringMap

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;
}
Also used : IndexedReadOnlyStringMap(org.apache.logging.log4j.util.IndexedReadOnlyStringMap) ReadOnlyStringMap(org.apache.logging.log4j.util.ReadOnlyStringMap) ArrayList(java.util.ArrayList) List(java.util.List) IndexedReadOnlyStringMap(org.apache.logging.log4j.util.IndexedReadOnlyStringMap)

Example 5 with IndexedReadOnlyStringMap

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");
}
Also used : Filter(org.apache.logging.log4j.core.Filter) Collection(java.util.Collection) IndexedReadOnlyStringMap(org.apache.logging.log4j.util.IndexedReadOnlyStringMap) Test(org.junit.jupiter.api.Test) LoggerContextSource(org.apache.logging.log4j.core.test.junit.LoggerContextSource)

Aggregations

IndexedReadOnlyStringMap (org.apache.logging.log4j.util.IndexedReadOnlyStringMap)8 Collection (java.util.Collection)3 List (java.util.List)3 Test (org.junit.jupiter.api.Test)3 ArrayList (java.util.ArrayList)2 Filter (org.apache.logging.log4j.core.Filter)2 LoggerContextSource (org.apache.logging.log4j.core.test.junit.LoggerContextSource)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Logger (org.apache.logging.log4j.Logger)1 ColumnMapping (org.apache.logging.log4j.core.appender.db.ColumnMapping)1 MapMessage (org.apache.logging.log4j.message.MapMessage)1 StringMapMessage (org.apache.logging.log4j.message.StringMapMessage)1 ReadOnlyStringMap (org.apache.logging.log4j.util.ReadOnlyStringMap)1 SortedArrayStringMap (org.apache.logging.log4j.util.SortedArrayStringMap)1 StringBuilderFormattable (org.apache.logging.log4j.util.StringBuilderFormattable)1 StringMap (org.apache.logging.log4j.util.StringMap)1