Search in sources :

Example 1 with KeyValuePair

use of org.apache.logging.log4j.core.util.KeyValuePair in project logging-log4j2 by apache.

the class MapFilterTest method testFilter.

@Test
public void testFilter() {
    final KeyValuePair[] pairs = new KeyValuePair[] { new KeyValuePair("FromAccount", "211000"), new KeyValuePair("ToAccount", "123456") };
    MapFilter filter = MapFilter.createFilter(pairs, "and", null, null);
    filter.start();
    MapMessage msg = new MapMessage();
    msg.put("ToAccount", "123456");
    msg.put("FromAccount", "211000");
    msg.put("Amount", "1000.00");
    assertTrue(filter.isStarted());
    assertSame(Filter.Result.NEUTRAL, filter.filter(null, Level.DEBUG, null, msg, null));
    msg.put("ToAccount", "111111");
    assertSame(Filter.Result.DENY, filter.filter(null, Level.ERROR, null, msg, null));
    filter = MapFilter.createFilter(pairs, "or", null, null);
    filter.start();
    msg = new MapMessage();
    msg.put("ToAccount", "123456");
    msg.put("FromAccount", "211000");
    msg.put("Amount", "1000.00");
    assertTrue(filter.isStarted());
    assertSame(Filter.Result.NEUTRAL, filter.filter(null, Level.DEBUG, null, msg, null));
    msg.put("ToAccount", "111111");
    assertSame(Filter.Result.NEUTRAL, filter.filter(null, Level.ERROR, null, msg, null));
}
Also used : KeyValuePair(org.apache.logging.log4j.core.util.KeyValuePair) MapMessage(org.apache.logging.log4j.message.MapMessage) Test(org.junit.Test)

Example 2 with KeyValuePair

use of org.apache.logging.log4j.core.util.KeyValuePair in project logging-log4j2 by apache.

the class DynamicThresholdFilterTest method testFilter.

@Test
public void testFilter() {
    ThreadContext.put("userid", "testuser");
    ThreadContext.put("organization", "apache");
    final KeyValuePair[] pairs = new KeyValuePair[] { new KeyValuePair("testuser", "DEBUG"), new KeyValuePair("JohnDoe", "warn") };
    final DynamicThresholdFilter filter = DynamicThresholdFilter.createFilter("userid", pairs, Level.ERROR, null, null);
    filter.start();
    assertTrue(filter.isStarted());
    assertSame(Filter.Result.NEUTRAL, filter.filter(null, Level.DEBUG, null, (Object) null, (Throwable) null));
    assertSame(Filter.Result.NEUTRAL, filter.filter(null, Level.ERROR, null, (Object) null, (Throwable) null));
    ThreadContext.clearMap();
    ThreadContext.put("userid", "JohnDoe");
    ThreadContext.put("organization", "apache");
    LogEvent event = Log4jLogEvent.newBuilder().setLevel(Level.DEBUG).setMessage(new SimpleMessage("Test")).build();
    assertSame(Filter.Result.DENY, filter.filter(event));
    event = Log4jLogEvent.newBuilder().setLevel(Level.ERROR).setMessage(new SimpleMessage("Test")).build();
    assertSame(Filter.Result.NEUTRAL, filter.filter(event));
    ThreadContext.clearMap();
}
Also used : KeyValuePair(org.apache.logging.log4j.core.util.KeyValuePair) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.Test)

Example 3 with KeyValuePair

use of org.apache.logging.log4j.core.util.KeyValuePair in project logging-log4j2 by apache.

the class MapFilter method createFilter.

@PluginFactory
public static MapFilter createFilter(@PluginElement("Pairs") final KeyValuePair[] pairs, @PluginAttribute("operator") final String oper, @PluginAttribute("onMatch") final Result match, @PluginAttribute("onMismatch") final Result mismatch) {
    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 = oper == null || !oper.equalsIgnoreCase("or");
    return new MapFilter(map, isAnd, match, mismatch);
}
Also used : KeyValuePair(org.apache.logging.log4j.core.util.KeyValuePair) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) PluginFactory(org.apache.logging.log4j.core.config.plugins.PluginFactory)

Example 4 with KeyValuePair

use of org.apache.logging.log4j.core.util.KeyValuePair in project logging-log4j2 by apache.

the class DynamicThresholdFilter method createFilter.

/**
     * Creates a DynamicThresholdFilter.
     * @param key The name of the key to compare.
     * @param pairs An array of value and Level pairs.
     * @param defaultThreshold The default Level.
     * @param onMatch The action to perform if a match occurs.
     * @param onMismatch The action to perform if no match occurs.
     * @return The DynamicThresholdFilter.
     */
@PluginFactory
public static DynamicThresholdFilter createFilter(@PluginAttribute("key") final String key, @PluginElement("Pairs") final KeyValuePair[] pairs, @PluginAttribute("defaultThreshold") final Level defaultThreshold, @PluginAttribute("onMatch") final Result onMatch, @PluginAttribute("onMismatch") final Result onMismatch) {
    final Map<String, Level> map = new HashMap<>();
    for (final KeyValuePair pair : pairs) {
        map.put(pair.getKey(), Level.toLevel(pair.getValue()));
    }
    final Level level = defaultThreshold == null ? Level.ERROR : defaultThreshold;
    return new DynamicThresholdFilter(key, map, level, onMatch, onMismatch);
}
Also used : KeyValuePair(org.apache.logging.log4j.core.util.KeyValuePair) HashMap(java.util.HashMap) Level(org.apache.logging.log4j.Level) PluginFactory(org.apache.logging.log4j.core.config.plugins.PluginFactory)

Example 5 with KeyValuePair

use of org.apache.logging.log4j.core.util.KeyValuePair in project logging-log4j2 by apache.

the class MapRewritePolicyTest method setupClass.

@BeforeClass
public static void setupClass() {
    map.put("test1", "one");
    map.put("test2", "two");
    logEvent0 = //
    Log4jLogEvent.newBuilder().setLoggerName(//
    "test").setContextMap(//
    map).setLoggerFqcn(//
    "MapRewritePolicyTest.setupClass()").setLevel(//
    Level.ERROR).setMessage(//
    new SimpleMessage("Test")).setThrown(//
    new RuntimeException("test")).setThreadName("none").setSource(new StackTraceElement("MapRewritePolicyTest", "setupClass", "MapRewritePolicyTest", 28)).setTimeMillis(2).build();
    logEvent1 = //
    ((Log4jLogEvent) logEvent0).asBuilder().setMessage(//
    new MapMessage(map)).setSource(//
    new StackTraceElement("MapRewritePolicyTest", "setupClass", "MapRewritePolicyTest", 29)).build();
    final ThreadContextStack stack = new MutableThreadContextStack(new ArrayList<>(map.values()));
    logEvent2 = //
    ((Log4jLogEvent) logEvent0).asBuilder().setContextStack(//
    stack).setMarker(//
    MarkerManager.getMarker("test")).setLevel(//
    Level.TRACE).setMessage(//
    new StructuredDataMessage("test", "Nothing", "test", map)).setTimeMillis(//
    20000000).setSource(//
    new StackTraceElement("MapRewritePolicyTest", "setupClass", "MapRewritePolicyTest", 30)).build();
    logEvent3 = //
    ((Log4jLogEvent) logEvent0).asBuilder().setContextStack(//
    stack).setLevel(//
    Level.ALL).setMessage(//
    new MapMessage(map)).setTimeMillis(//
    Long.MAX_VALUE).setSource(//
    new StackTraceElement("MapRewritePolicyTest", "setupClass", "MapRewritePolicyTest", 31)).build();
    rewrite = new KeyValuePair[] { new KeyValuePair("test2", "2"), new KeyValuePair("test3", "three") };
}
Also used : MutableThreadContextStack(org.apache.logging.log4j.spi.MutableThreadContextStack) ThreadContextStack(org.apache.logging.log4j.spi.ThreadContextStack) StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) KeyValuePair(org.apache.logging.log4j.core.util.KeyValuePair) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) MapMessage(org.apache.logging.log4j.message.MapMessage) MutableThreadContextStack(org.apache.logging.log4j.spi.MutableThreadContextStack) BeforeClass(org.junit.BeforeClass)

Aggregations

KeyValuePair (org.apache.logging.log4j.core.util.KeyValuePair)15 Test (org.junit.Test)7 HashMap (java.util.HashMap)5 PluginFactory (org.apache.logging.log4j.core.config.plugins.PluginFactory)5 ListAppender (org.apache.logging.log4j.test.appender.ListAppender)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Appender (org.apache.logging.log4j.core.Appender)3 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)3 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)3 LogEvent (org.apache.logging.log4j.core.LogEvent)2 MapMessage (org.apache.logging.log4j.message.MapMessage)2 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 InflaterInputStream (java.util.zip.InflaterInputStream)1 Level (org.apache.logging.log4j.Level)1 JavaLookup (org.apache.logging.log4j.core.lookup.JavaLookup)1