Search in sources :

Example 11 with Filter

use of org.apache.log4j.spi.Filter in project logging-log4j2 by apache.

the class FilterAdapter method filter.

@Override
public Result filter(LogEvent event) {
    LoggingEvent loggingEvent = new LogEventAdapter(event);
    Filter next = filter;
    while (next != null) {
        switch(filter.decide(loggingEvent)) {
            case Filter.ACCEPT:
                return Result.ACCEPT;
            case Filter.DENY:
                return Result.DENY;
            default:
        }
        next = filter.getNext();
    }
    return Result.NEUTRAL;
}
Also used : LoggingEvent(org.apache.log4j.spi.LoggingEvent) AbstractFilter(org.apache.logging.log4j.core.filter.AbstractFilter) Filter(org.apache.log4j.spi.Filter)

Example 12 with Filter

use of org.apache.log4j.spi.Filter in project logging-log4j2 by apache.

the class LevelMatchFilterBuilder method createFilter.

private Filter createFilter(String level, boolean acceptOnMatch) {
    Level lvl = Level.ERROR;
    if (level != null) {
        lvl = Level.toLevel(level, Level.ERROR);
    }
    org.apache.logging.log4j.core.Filter.Result onMatch = acceptOnMatch ? org.apache.logging.log4j.core.Filter.Result.ACCEPT : org.apache.logging.log4j.core.Filter.Result.DENY;
    return new FilterWrapper(LevelMatchFilter.newBuilder().setLevel(lvl).setOnMatch(onMatch).setOnMismatch(org.apache.logging.log4j.core.Filter.Result.NEUTRAL).build());
}
Also used : FilterWrapper(org.apache.log4j.bridge.FilterWrapper) Filter(org.apache.log4j.spi.Filter) LevelMatchFilter(org.apache.logging.log4j.core.filter.LevelMatchFilter) Level(org.apache.logging.log4j.Level)

Example 13 with Filter

use of org.apache.log4j.spi.Filter in project logging-log4j2 by apache.

the class SyslogAppenderBuilder method parseAppender.

@Override
public Appender parseAppender(final String name, final String appenderPrefix, final String layoutPrefix, final String filterPrefix, final Properties props, final PropertiesConfiguration configuration) {
    Filter filter = configuration.parseAppenderFilters(props, filterPrefix, name);
    Layout layout = configuration.parseLayout(layoutPrefix, name, props);
    String level = getProperty(THRESHOLD_PARAM);
    String facility = getProperty(FACILITY_PARAM, DEFAULT_FACILITY);
    String syslogHost = getProperty(SYSLOG_HOST_PARAM, DEFAULT_HOST + ":" + DEFAULT_PORT);
    String protocol = getProperty(PROTOCOL_PARAM, Protocol.TCP.name());
    return createAppender(name, configuration, layout, facility, filter, syslogHost, level, Protocol.valueOf(protocol));
}
Also used : Filter(org.apache.log4j.spi.Filter) SyslogLayout(org.apache.logging.log4j.core.layout.SyslogLayout) Layout(org.apache.log4j.Layout)

Example 14 with Filter

use of org.apache.log4j.spi.Filter in project xtext-core by eclipse.

the class LoggingTester method removeFilter.

private static void removeFilter(Appender appender, Filter filter) {
    if (Objects.equals(appender.getFilter(), filter)) {
        appender.clearFilters();
        appender.addFilter(filter.getNext());
    } else {
        for (Filter current = appender.getFilter(); current != null; current = current.getNext()) {
            if (Objects.equals(current.getNext(), filter)) {
                current.setNext(filter.getNext());
                return;
            }
        }
    }
}
Also used : Filter(org.apache.log4j.spi.Filter)

Example 15 with Filter

use of org.apache.log4j.spi.Filter in project knime-core by knime.

the class NodeLogger method updateLog4JKNIMELoggerLevel.

/**
 * Adjusts log level of 'knime' loggers so that it matches the minimum level of all registered appenders.
 * Called after initialization and after the log level is changed for individual appenders.
 */
private static void updateLog4JKNIMELoggerLevel() {
    final Logger rootLogger = LogManager.getRootLogger();
    // by default this is 'ERROR' but may be changed in log4j.xml
    Level minimumLevel = rootLogger.getLevel();
    for (@SuppressWarnings("unchecked") Enumeration<Appender> appenderEnum = rootLogger.getAllAppenders(); appenderEnum.hasMoreElements(); ) {
        Appender next = appenderEnum.nextElement();
        for (Filter filter = next.getFilter(); filter != null; filter = filter.getNext()) {
            Level l = null;
            if (filter instanceof LevelMatchFilter) {
                l = OptionConverter.toLevel(((LevelMatchFilter) filter).getLevelToMatch(), Level.FATAL);
            } else if (filter instanceof LevelRangeFilter) {
                l = ((LevelRangeFilter) filter).getLevelMin();
            }
            if (l != null && minimumLevel.isGreaterOrEqual(l)) {
                minimumLevel = l;
            }
        }
    }
    final Level minimumLevelFinal = minimumLevel;
    synchronized (LOGGERS) {
        knownLoggerPrefixes.stream().map(LogManager::getLogger).forEach(l -> l.setLevel(minimumLevelFinal));
    }
}
Also used : WriterAppender(org.apache.log4j.WriterAppender) LogfileAppender(org.knime.core.util.LogfileAppender) NullAppender(org.apache.log4j.varia.NullAppender) Appender(org.apache.log4j.Appender) FileAppender(org.apache.log4j.FileAppender) LevelRangeFilter(org.apache.log4j.varia.LevelRangeFilter) Filter(org.apache.log4j.spi.Filter) LevelMatchFilter(org.apache.log4j.varia.LevelMatchFilter) LevelRangeFilter(org.apache.log4j.varia.LevelRangeFilter) Level(org.apache.log4j.Level) Logger(org.apache.log4j.Logger) LevelMatchFilter(org.apache.log4j.varia.LevelMatchFilter)

Aggregations

Filter (org.apache.log4j.spi.Filter)31 Appender (org.apache.log4j.Appender)14 Layout (org.apache.log4j.Layout)9 LoggingEvent (org.apache.log4j.spi.LoggingEvent)7 ArrayList (java.util.ArrayList)5 WriterAppender (org.apache.log4j.WriterAppender)5 Enumeration (java.util.Enumeration)4 List (java.util.List)4 Logger (org.apache.log4j.Logger)4 FileAppender (org.apache.log4j.FileAppender)3 PatternLayout (org.apache.log4j.PatternLayout)3 FilterWrapper (org.apache.log4j.bridge.FilterWrapper)3 PropertySetter (org.apache.log4j.config.PropertySetter)3 ErrorHandler (org.apache.log4j.spi.ErrorHandler)3 LevelMatchFilter (org.apache.log4j.varia.LevelMatchFilter)3 LevelRangeFilter (org.apache.log4j.varia.LevelRangeFilter)3 NullAppender (org.apache.log4j.varia.NullAppender)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2