Search in sources :

Example 1 with CompositeFilter

use of org.apache.logging.log4j.core.filter.CompositeFilter in project logging-log4j2 by apache.

the class CustomLevelsWithFiltersTest method testConfiguration.

@Test
public void testConfiguration() {
    final Configuration configuration = context.getConfiguration();
    assertNotNull(configuration);
    final FileAppender appender = configuration.getAppender("info");
    assertNotNull(appender);
    final CompositeFilter compFilter = (CompositeFilter) appender.getFilter();
    assertNotNull(compFilter);
    final Filter[] filters = compFilter.getFiltersArray();
    assertNotNull(filters);
    boolean foundLevel = false;
    for (final Filter filter : filters) {
        final ThresholdFilter tFilter = (ThresholdFilter) filter;
        if (infom1Level.equals(tFilter.getLevel())) {
            foundLevel = true;
            break;
        }
    }
    Assert.assertTrue("Level not found: " + infom1Level, foundLevel);
}
Also used : FileAppender(org.apache.logging.log4j.core.appender.FileAppender) ThresholdFilter(org.apache.logging.log4j.core.filter.ThresholdFilter) CompositeFilter(org.apache.logging.log4j.core.filter.CompositeFilter) Configuration(org.apache.logging.log4j.core.config.Configuration) CompositeFilter(org.apache.logging.log4j.core.filter.CompositeFilter) ThresholdFilter(org.apache.logging.log4j.core.filter.ThresholdFilter) Test(org.junit.Test)

Example 2 with CompositeFilter

use of org.apache.logging.log4j.core.filter.CompositeFilter in project logging-log4j2 by apache.

the class TestConfigurator method testNoFilters.

@Test
public void testNoFilters() throws Exception {
    ctx = Configurator.initialize("Test1", "bad/log4j-nofilter.xml");
    LogManager.getLogger("org.apache.test.TestConfigurator");
    final Configuration config = ctx.getConfiguration();
    assertNotNull(config, "No configuration");
    assertEquals("XMLConfigTest", config.getName(), "Unexpected Configuration");
    final LoggerConfig lcfg = config.getLoggerConfig("org.apache.logging.log4j.test1");
    assertNotNull(lcfg, "No Logger");
    final Filter filter = lcfg.getFilter();
    assertNotNull(filter, "No Filter");
    assertThat(filter, instanceOf(CompositeFilter.class));
    assertTrue(((CompositeFilter) filter).isEmpty(), "Unexpected filters");
}
Also used : CompositeFilter(org.apache.logging.log4j.core.filter.CompositeFilter) BuiltConfiguration(org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration) CompositeFilter(org.apache.logging.log4j.core.filter.CompositeFilter) Filter(org.apache.logging.log4j.core.Filter) Test(org.junit.jupiter.api.Test)

Example 3 with CompositeFilter

use of org.apache.logging.log4j.core.filter.CompositeFilter in project SpongeCommon by SpongePowered.

the class MixinLoggerInjector method captureLogger.

public static Queue<String> captureLogger() {
    if (!MixinEnvironment.getDefaultEnvironment().getOption(MixinEnvironment.Option.DEBUG_VERBOSE)) {
        throw new IllegalStateException("Mixin capture will not function appropriately unless -Dmixin.debug.verbose is enabled");
    }
    final Logger mixinLogger = (Logger) LogManager.getLogger("mixin");
    final CompositeFilter messageFilter = CompositeFilter.createFilters(new Filter[] { // regex patterns
    MixinLoggerInjector.pattern(".*for final field [^@]+@Mutable.*"), // MixinLoggerInjector.pattern("^NESTING not supported.*"), // todo: if we need to
    new DenyAllFilter() });
    final CaptureAppender appender = CaptureAppender.builder().setName("IntegrationTest-Capture").setFilter(messageFilter).build();
    appender.start();
    mixinLogger.addAppender(appender);
    return appender.messages();
}
Also used : CompositeFilter(org.apache.logging.log4j.core.filter.CompositeFilter) Logger(org.apache.logging.log4j.core.Logger)

Example 4 with CompositeFilter

use of org.apache.logging.log4j.core.filter.CompositeFilter in project logging-log4j2 by apache.

the class CustomLevelsWithFiltersTest method testConfiguration.

@Test
public void testConfiguration(final Configuration configuration, @Named("info") final FileAppender appender) {
    assertNotNull(configuration);
    assertNotNull(appender);
    final CompositeFilter compFilter = (CompositeFilter) appender.getFilter();
    assertNotNull(compFilter);
    final Filter[] filters = compFilter.getFiltersArray();
    assertNotNull(filters);
    boolean foundLevel = false;
    for (final Filter filter : filters) {
        final ThresholdFilter tFilter = (ThresholdFilter) filter;
        if (infom1Level.equals(tFilter.getLevel())) {
            foundLevel = true;
            break;
        }
    }
    assertTrue(foundLevel, "Level not found: " + infom1Level);
}
Also used : ThresholdFilter(org.apache.logging.log4j.core.filter.ThresholdFilter) CompositeFilter(org.apache.logging.log4j.core.filter.CompositeFilter) CompositeFilter(org.apache.logging.log4j.core.filter.CompositeFilter) ThresholdFilter(org.apache.logging.log4j.core.filter.ThresholdFilter) Test(org.junit.jupiter.api.Test)

Example 5 with CompositeFilter

use of org.apache.logging.log4j.core.filter.CompositeFilter 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

CompositeFilter (org.apache.logging.log4j.core.filter.CompositeFilter)5 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)2 Configuration (org.apache.logging.log4j.core.config.Configuration)2 ThresholdFilter (org.apache.logging.log4j.core.filter.ThresholdFilter)2 Test (org.junit.Test)2 Test (org.junit.jupiter.api.Test)2 ListAppender (org.apache.log4j.ListAppender)1 FilterAdapter (org.apache.log4j.bridge.FilterAdapter)1 Appender (org.apache.logging.log4j.core.Appender)1 Filter (org.apache.logging.log4j.core.Filter)1 Logger (org.apache.logging.log4j.core.Logger)1 LoggerContext (org.apache.logging.log4j.core.LoggerContext)1 BuiltConfiguration (org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration)1 Filterable (org.apache.logging.log4j.core.filter.Filterable)1 LevelRangeFilter (org.apache.logging.log4j.core.filter.LevelRangeFilter)1