Search in sources :

Example 1 with FilterAdapter

use of org.apache.log4j.bridge.FilterAdapter in project logging-log4j2 by apache.

the class PropertiesConfigurationTest method testConsoleAppenderFilter.

@Test
public void testConsoleAppenderFilter() throws Exception {
    try (LoggerContext loggerContext = TestConfigurator.configure("target/test-classes/LOG4J2-3247.properties")) {
        // LOG4J2-3281 PropertiesConfiguration.buildAppender not adding filters to appender
        final Configuration configuration = loggerContext.getConfiguration();
        assertNotNull(configuration);
        final Appender appender = configuration.getAppender("CONSOLE");
        assertNotNull(appender);
        final Filterable filterable = (Filterable) appender;
        final FilterAdapter filter = (FilterAdapter) filterable.getFilter();
        assertNotNull(filter);
        assertTrue(filter.getFilter() instanceof NeutralFilterFixture);
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.log4j.ListAppender) FileAppender(org.apache.logging.log4j.core.appender.FileAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) Filterable(org.apache.logging.log4j.core.filter.Filterable) FilterAdapter(org.apache.log4j.bridge.FilterAdapter) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Test(org.junit.Test)

Example 2 with FilterAdapter

use of org.apache.log4j.bridge.FilterAdapter in project logging-log4j2 by apache.

the class PropertiesConfigurationTest method testCustomAppenderFilter.

@Test
public void testCustomAppenderFilter() throws Exception {
    try (LoggerContext loggerContext = TestConfigurator.configure("target/test-classes/LOG4J2-3281.properties")) {
        // LOG4J2-3281 PropertiesConfiguration.buildAppender not adding filters to appender
        final Configuration configuration = loggerContext.getConfiguration();
        assertNotNull(configuration);
        final Appender appender = configuration.getAppender("CUSTOM");
        assertNotNull(appender);
        final Filterable filterable = (Filterable) appender;
        final FilterAdapter filter = (FilterAdapter) filterable.getFilter();
        assertNotNull(filter);
        assertTrue(filter.getFilter() instanceof NeutralFilterFixture);
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.log4j.ListAppender) FileAppender(org.apache.logging.log4j.core.appender.FileAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) Filterable(org.apache.logging.log4j.core.filter.Filterable) FilterAdapter(org.apache.log4j.bridge.FilterAdapter) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Test(org.junit.Test)

Example 3 with FilterAdapter

use of org.apache.log4j.bridge.FilterAdapter in project logging-log4j2 by apache.

the class AbstractBuilder method buildFilters.

protected org.apache.logging.log4j.core.Filter buildFilters(final String level, final Filter filter) {
    if (level != null && filter != null) {
        final List<org.apache.logging.log4j.core.Filter> filterList = new ArrayList<>();
        final org.apache.logging.log4j.core.Filter thresholdFilter = ThresholdFilter.createFilter(OptionConverter.convertLevel(level, Level.TRACE), org.apache.logging.log4j.core.Filter.Result.NEUTRAL, org.apache.logging.log4j.core.Filter.Result.DENY);
        filterList.add(thresholdFilter);
        Filter f = filter;
        while (f != null) {
            if (filter instanceof FilterWrapper) {
                filterList.add(((FilterWrapper) f).getFilter());
            } else {
                filterList.add(new FilterAdapter(f));
            }
            f = f.getNext();
        }
        return CompositeFilter.createFilters(filterList.toArray(org.apache.logging.log4j.core.Filter.EMPTY_ARRAY));
    } else if (level != null) {
        return ThresholdFilter.createFilter(OptionConverter.convertLevel(level, Level.TRACE), org.apache.logging.log4j.core.Filter.Result.NEUTRAL, org.apache.logging.log4j.core.Filter.Result.DENY);
    } else if (filter != null) {
        if (filter instanceof FilterWrapper) {
            return ((FilterWrapper) filter).getFilter();
        } else {
            return new FilterAdapter(filter);
        }
    }
    return null;
}
Also used : FilterWrapper(org.apache.log4j.bridge.FilterWrapper) CompositeFilter(org.apache.logging.log4j.core.filter.CompositeFilter) ThresholdFilter(org.apache.logging.log4j.core.filter.ThresholdFilter) Filter(org.apache.log4j.spi.Filter) ArrayList(java.util.ArrayList) FilterAdapter(org.apache.log4j.bridge.FilterAdapter)

Example 4 with FilterAdapter

use of org.apache.log4j.bridge.FilterAdapter in project logging-log4j2 by apache.

the class PropertiesConfigurationTest method testConsoleAppenderLevelRangeFilter.

@Test
public void testConsoleAppenderLevelRangeFilter() throws Exception {
    try (LoggerContext loggerContext = TestConfigurator.configure("target/test-classes/LOG4J2-3326.properties")) {
        final Configuration configuration = loggerContext.getConfiguration();
        assertNotNull(configuration);
        final Appender appender = configuration.getAppender("CUSTOM");
        assertNotNull(appender);
        final Filterable filterable = (Filterable) appender;
        final CompositeFilter filter = (CompositeFilter) filterable.getFilter();
        final org.apache.logging.log4j.core.Filter[] filters = filter.getFiltersArray();
        final LevelRangeFilter customFilterReal = (LevelRangeFilter) ((FilterWrapper) ((FilterAdapter) filters[0]).getFilter()).getFilter();
        assertEquals(Level.ALL, customFilterReal.getMinLevel());
        final LevelRangeFilter defaultFilter = (LevelRangeFilter) ((FilterWrapper) ((FilterAdapter) filters[1]).getFilter()).getFilter();
        assertEquals(Level.TRACE, defaultFilter.getMinLevel());
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.log4j.ListAppender) FileAppender(org.apache.logging.log4j.core.appender.FileAppender) CompositeFilter(org.apache.logging.log4j.core.filter.CompositeFilter) Configuration(org.apache.logging.log4j.core.config.Configuration) CompositeFilter(org.apache.logging.log4j.core.filter.CompositeFilter) LevelRangeFilter(org.apache.logging.log4j.core.filter.LevelRangeFilter) Filterable(org.apache.logging.log4j.core.filter.Filterable) LevelRangeFilter(org.apache.logging.log4j.core.filter.LevelRangeFilter) FilterAdapter(org.apache.log4j.bridge.FilterAdapter) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Test(org.junit.Test)

Aggregations

FilterAdapter (org.apache.log4j.bridge.FilterAdapter)4 ListAppender (org.apache.log4j.ListAppender)3 Appender (org.apache.logging.log4j.core.Appender)3 LoggerContext (org.apache.logging.log4j.core.LoggerContext)3 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)3 Configuration (org.apache.logging.log4j.core.config.Configuration)3 Filterable (org.apache.logging.log4j.core.filter.Filterable)3 Test (org.junit.Test)3 CompositeFilter (org.apache.logging.log4j.core.filter.CompositeFilter)2 ArrayList (java.util.ArrayList)1 FilterWrapper (org.apache.log4j.bridge.FilterWrapper)1 Filter (org.apache.log4j.spi.Filter)1 LevelRangeFilter (org.apache.logging.log4j.core.filter.LevelRangeFilter)1 ThresholdFilter (org.apache.logging.log4j.core.filter.ThresholdFilter)1