Search in sources :

Example 6 with PluginFactory

use of org.apache.logging.log4j.plugins.PluginFactory in project logging-log4j2 by apache.

the class StructuredDataFilter method createFilter.

/**
 * 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);
}
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.plugins.PluginFactory)

Example 7 with PluginFactory

use of org.apache.logging.log4j.plugins.PluginFactory in project logging-log4j2 by apache.

the class ThresholdFilter method createFilter.

/**
 * Creates a ThresholdFilter.
 * @param level The log Level.
 * @param match The action to take on a match.
 * @param mismatch The action to take on a mismatch.
 * @return The created ThresholdFilter.
 */
// TODO Consider refactoring to use AbstractFilter.AbstractFilterBuilder
@PluginFactory
public static ThresholdFilter createFilter(@PluginAttribute final Level level, @PluginAttribute("onMatch") final Result match, @PluginAttribute("onMismatch") final Result mismatch) {
    final Level actualLevel = level == null ? Level.ERROR : level;
    final Result onMatch = match == null ? Result.NEUTRAL : match;
    final Result onMismatch = mismatch == null ? Result.DENY : mismatch;
    return new ThresholdFilter(actualLevel, onMatch, onMismatch);
}
Also used : Level(org.apache.logging.log4j.Level) PluginFactory(org.apache.logging.log4j.plugins.PluginFactory)

Example 8 with PluginFactory

use of org.apache.logging.log4j.plugins.PluginFactory in project logging-log4j2 by apache.

the class LevelRangeFilter method createFilter.

/**
 * Creates a ThresholdFilter.
 *
 * @param minLevel
 *            The minimum log Level.
 * @param maxLevel
 *            The maximum log Level.
 * @param match
 *            The action to take on a match.
 * @param mismatch
 *            The action to take on a mismatch.
 * @return The created ThresholdFilter.
 */
// TODO Consider refactoring to use AbstractFilter.AbstractFilterBuilder
@PluginFactory
public static LevelRangeFilter createFilter(// @formatter:off
@PluginAttribute final Level minLevel, @PluginAttribute final Level maxLevel, @PluginAttribute final Result match, @PluginAttribute final Result mismatch) {
    // @formatter:on
    final Level actualMinLevel = minLevel == null ? Level.ERROR : minLevel;
    final Level actualMaxLevel = maxLevel == null ? Level.ERROR : maxLevel;
    final Result onMatch = match == null ? Result.NEUTRAL : match;
    final Result onMismatch = mismatch == null ? Result.DENY : mismatch;
    return new LevelRangeFilter(actualMinLevel, actualMaxLevel, onMatch, onMismatch);
}
Also used : Level(org.apache.logging.log4j.Level) PluginFactory(org.apache.logging.log4j.plugins.PluginFactory)

Example 9 with PluginFactory

use of org.apache.logging.log4j.plugins.PluginFactory in project logging-log4j2 by apache.

the class ThreadContextMapFilter method createFilter.

// TODO Consider refactoring to use AbstractFilter.AbstractFilterBuilder
@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);
}
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.plugins.PluginFactory)

Example 10 with PluginFactory

use of org.apache.logging.log4j.plugins.PluginFactory in project logging-log4j2 by apache.

the class CronTriggeringPolicy method createPolicy.

/**
 * Creates a ScheduledTriggeringPolicy.
 *
 * @param configuration
 *            the Configuration.
 * @param evaluateOnStartup
 *            check if the file should be rolled over immediately.
 * @param schedule
 *            the cron expression.
 * @return a ScheduledTriggeringPolicy.
 */
@PluginFactory
public static CronTriggeringPolicy createPolicy(@PluginConfiguration final Configuration configuration, @PluginAttribute final String evaluateOnStartup, @PluginAttribute final String schedule) {
    CronExpression cronExpression;
    final boolean checkOnStartup = Boolean.parseBoolean(evaluateOnStartup);
    if (schedule == null) {
        LOGGER.info("No schedule specified, defaulting to Daily");
        cronExpression = getSchedule(defaultSchedule);
    } else {
        cronExpression = getSchedule(schedule);
        if (cronExpression == null) {
            LOGGER.error("Invalid expression specified. Defaulting to Daily");
            cronExpression = getSchedule(defaultSchedule);
        }
    }
    return new CronTriggeringPolicy(cronExpression, checkOnStartup, configuration);
}
Also used : CronExpression(org.apache.logging.log4j.core.util.CronExpression) PluginFactory(org.apache.logging.log4j.plugins.PluginFactory)

Aggregations

PluginFactory (org.apache.logging.log4j.plugins.PluginFactory)13 HashMap (java.util.HashMap)5 KeyValuePair (org.apache.logging.log4j.core.util.KeyValuePair)5 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Level (org.apache.logging.log4j.Level)3 Method (java.lang.reflect.Method)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 PrintWriter (java.io.PrintWriter)1 Reader (java.io.Reader)1 URI (java.net.URI)1 Charset (java.nio.charset.Charset)1 Path (java.nio.file.Path)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 TimeUnit (java.util.concurrent.TimeUnit)1 DataSource (javax.sql.DataSource)1