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

The following examples show how to use org.apache.logging.log4j.core.util.KeyValuePair#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: EcsLayout.java    From ecs-logging-java with Apache License 2.0 5 votes vote down vote up
private void serializeAdditionalFieldsAndMDC(LogEvent event, StringBuilder builder) {
    final int length = additionalFields.length;
    if (!event.getContextData().isEmpty() || length > 0) {
        if (length > 0) {
            final StrSubstitutor strSubstitutor = getConfiguration().getStrSubstitutor();
            for (int i = 0; i < length; i++) {
                KeyValuePair additionalField = additionalFields[i];
                PatternFormatter[] formatters = fieldValuePatternFormatter[i];
                CharSequence value = null;
                if (formatters != null) {
                    StringBuilder buffer = EcsJsonSerializer.getMessageStringBuilder();
                    formatPattern(event, formatters, buffer);
                    if (buffer.length() > 0) {
                        value = buffer;
                    }
                } else if (valueNeedsLookup(additionalField.getValue())) {
                    StringBuilder lookupValue = EcsJsonSerializer.getMessageStringBuilder();
                    lookupValue.append(additionalField.getValue());
                    if (strSubstitutor.replaceIn(event, lookupValue)) {
                        value = lookupValue;
                    }
                } else {
                    value = additionalField.getValue();
                }

                if (value != null) {
                    builder.append('\"');
                    JsonUtils.quoteAsString(additionalField.getKey(), builder);
                    builder.append("\":\"");
                    JsonUtils.quoteAsString(EcsJsonSerializer.toNullSafeString(value), builder);
                    builder.append("\",");
                }
            }
        }
        event.getContextData().forEach(WRITE_MDC, builder);
    }
}
 
Example 2
Source File: MapRewritePolicy.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * The factory method to create the MapRewritePolicy.
 * @param mode The string representation of the Mode.
 * @param pairs key/value pairs for the new Map keys and values.
 * @return The MapRewritePolicy.
 */
@PluginFactory
public static MapRewritePolicy createPolicy(
        @PluginAttribute final String mode,
        @PluginElement("KeyValuePair") final KeyValuePair[] pairs) {
    Mode op = mode == null ? op = Mode.Add : Mode.valueOf(mode);
    if (pairs == null || pairs.length == 0) {
        LOGGER.error("keys and values must be specified for the MapRewritePolicy");
        return null;
    }
    final Map<String, Object> map = new HashMap<>();
    for (final KeyValuePair pair : pairs) {
        final String key = pair.getKey();
        if (key == null) {
            LOGGER.error("A null key is not valid in MapRewritePolicy");
            continue;
        }
        final String value = pair.getValue();
        if (value == null) {
            LOGGER.error("A null value for key " + key + " is not allowed in MapRewritePolicy");
            continue;
        }
        map.put(pair.getKey(), pair.getValue());
    }
    if (map.isEmpty()) {
        LOGGER.error("MapRewritePolicy is not configured with any valid key value pairs");
        return null;
    }
    return new MapRewritePolicy(map, op);
}
 
Example 3
Source File: ThreadContextMapFilter.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@PluginFactory
public static ThreadContextMapFilter createFilter(
        @PluginElement final KeyValuePair[] pairs,
        @PluginAttribute final String operator,
        @PluginAttribute final Result onMatch,
        @PluginAttribute final Result onMismatch) {
    if (pairs == null || pairs.length == 0) {
        LOGGER.error("key and value pairs must be specified for the ThreadContextMapFilter");
        return null;
    }
    final Map<String, List<String>> map = new HashMap<>();
    for (final KeyValuePair pair : pairs) {
        final String key = pair.getKey();
        if (key == null) {
            LOGGER.error("A null key is not valid in MapFilter");
            continue;
        }
        final String value = pair.getValue();
        if (value == null) {
            LOGGER.error("A null value for key " + key + " is not allowed in MapFilter");
            continue;
        }
        List<String> list = map.get(pair.getKey());
        if (list != null) {
            list.add(value);
        } else {
            list = new ArrayList<>();
            list.add(value);
            map.put(pair.getKey(), list);
        }
    }
    if (map.isEmpty()) {
        LOGGER.error("ThreadContextMapFilter is not configured with any valid key value pairs");
        return null;
    }
    final boolean isAnd = operator == null || !operator.equalsIgnoreCase("or");
    return new ThreadContextMapFilter(map, isAnd, onMatch, onMismatch);
}
 
Example 4
Source File: MapFilter.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@PluginFactory
public static MapFilter createFilter(
        @PluginElement final KeyValuePair[] pairs,
        @PluginAttribute final String operator,
        @PluginAttribute final Result onMatch,
        @PluginAttribute final Result onMismatch) {
    if (pairs == null || pairs.length == 0) {
        LOGGER.error("keys and values must be specified for the MapFilter");
        return null;
    }
    final Map<String, List<String>> map = new HashMap<>();
    for (final KeyValuePair pair : pairs) {
        final String key = pair.getKey();
        if (key == null) {
            LOGGER.error("A null key is not valid in MapFilter");
            continue;
        }
        final String value = pair.getValue();
        if (value == null) {
            LOGGER.error("A null value for key " + key + " is not allowed in MapFilter");
            continue;
        }
        List<String> list = map.get(pair.getKey());
        if (list != null) {
            list.add(value);
        } else {
            list = new ArrayList<>();
            list.add(value);
            map.put(pair.getKey(), list);
        }
    }
    if (map.isEmpty()) {
        LOGGER.error("MapFilter is not configured with any valid key value pairs");
        return null;
    }
    final boolean isAnd = operator == null || !operator.equalsIgnoreCase("or");
    return new MapFilter(map, isAnd, onMatch, onMismatch);
}
 
Example 5
Source File: StructuredDataFilter.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the StructuredDataFilter.
 * @param pairs Key and value pairs.
 * @param operator The operator to perform. If not "or" the operation will be an "and".
 * @param onMatch The action to perform on a match.
 * @param onMismatch The action to perform on a mismatch.
 * @return The StructuredDataFilter.
 */
// TODO Consider refactoring to use AbstractFilter.AbstractFilterBuilder
@PluginFactory
public static StructuredDataFilter createFilter(
        @PluginElement final KeyValuePair[] pairs,
        @PluginAttribute final String operator,
        @PluginAttribute final Result onMatch,
        @PluginAttribute final Result onMismatch) {
    if (pairs == null || pairs.length == 0) {
        LOGGER.error("keys and values must be specified for the StructuredDataFilter");
        return null;
    }
    final Map<String, List<String>> map = new HashMap<>();
    for (final KeyValuePair pair : pairs) {
        final String key = pair.getKey();
        if (key == null) {
            LOGGER.error("A null key is not valid in MapFilter");
            continue;
        }
        final String value = pair.getValue();
        if (value == null) {
            LOGGER.error("A null value for key " + key + " is not allowed in MapFilter");
            continue;
        }
        List<String> list = map.get(pair.getKey());
        if (list != null) {
            list.add(value);
        } else {
            list = new ArrayList<>();
            list.add(value);
            map.put(pair.getKey(), list);
        }
    }
    if (map.isEmpty()) {
        LOGGER.error("StructuredDataFilter is not configured with any valid key value pairs");
        return null;
    }
    final boolean isAnd = operator == null || !operator.equalsIgnoreCase("or");
    return new StructuredDataFilter(map, isAnd, onMatch, onMismatch);
}
 
Example 6
Source File: AbstractJacksonLayout.java    From curiostack with MIT License 4 votes vote down vote up
ResolvableKeyValuePair(final KeyValuePair pair) {
  this.key = pair.getKey();
  this.value = pair.getValue();
  this.valueNeedsLookup = AbstractJacksonLayout.valueNeedsLookup(this.value);
}
 
Example 7
Source File: AbstractJacksonLayout.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
ResolvableKeyValuePair(final KeyValuePair pair) {
    this.key = pair.getKey();
    this.value = pair.getValue();
    this.valueNeedsLookup = AbstractJacksonLayout.valueNeedsLookup(this.value);
}
 
Example 8
Source File: AbstractJacksonLayout.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
ResolvableKeyValuePair(final KeyValuePair pair) {
    this.key = pair.getKey();
    this.value = pair.getValue();
    this.valueNeedsLookup = AbstractJacksonLayout.valueNeedsLookup(this.value);
}