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));
}
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();
}
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);
}
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);
}
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") };
}
Aggregations