Java Code Examples for org.apache.logging.log4j.util.ReadOnlyStringMap#getValue()

The following examples show how to use org.apache.logging.log4j.util.ReadOnlyStringMap#getValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MdcPatternConverter.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
private static void appendSelectedKeys(final String[] keys, final ReadOnlyStringMap contextData, final StringBuilder sb) {
    // Print all the keys in the array that have a value.
    final int start = sb.length();
    sb.append('{');
    for (int i = 0; i < keys.length; i++) {
        final String theKey = keys[i];
        final Object value = contextData.getValue(theKey);
        if (value != null) { // !contextData.containskey(theKey)
            if (sb.length() - start > 1) {
                sb.append(", ");
            }
            sb.append(theKey).append('=');
            StringBuilders.appendValue(sb, value);
        }
    }
    sb.append('}');
}
 
Example 2
Source File: ThreadContextMapFilter.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: MdcPatternConverter.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void format(final LogEvent event, final StringBuilder toAppendTo) {
    final ReadOnlyStringMap contextData = event.getContextData();
    // if there is no additional options, we output every single
    // Key/Value pair for the MDC in a similar format to Hashtable.toString()
    if (full) {
        if (contextData == null || contextData.size() == 0) {
            toAppendTo.append("{}");
            return;
        }
        appendFully(contextData, toAppendTo);
    } else {
        if (keys != null) {
            if (contextData == null || contextData.size() == 0) {
                toAppendTo.append("{}");
                return;
            }
            appendSelectedKeys(keys, contextData, toAppendTo);
        } else if (contextData != null){
            // otherwise they just want a single key output
            final Object value = contextData.getValue(key);
            if (value != null) {
                StringBuilders.appendValue(toAppendTo, value);
            }
        }
    }
}
 
Example 4
Source File: DynamicThresholdFilter.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private Result filter(final Level level, final ReadOnlyStringMap contextMap) {
    final String value = contextMap.getValue(key);
    if (value != null) {
        Level ctxLevel = levelMap.get(value);
        if (ctxLevel == null) {
            ctxLevel = defaultThreshold;
        }
        return level.isMoreSpecificThan(ctxLevel) ? onMatch : onMismatch;
    }
    return Result.NEUTRAL;

}
 
Example 5
Source File: MapFilter.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
protected boolean filter(final ReadOnlyStringMap data) {
    boolean match = false;
    for (int i = 0; i < map.size(); i++) {
        final String toMatch = data.getValue(map.getKeyAt(i));
        match = toMatch != null && ((List<String>) map.getValueAt(i)).contains(toMatch);

        if ((!isAnd && match) || (isAnd && !match)) {
            break;
        }
    }
    return match;
}