Search in sources :

Example 1 with Filter

use of ch.qos.logback.core.filter.Filter in project sling by apache.

the class ITFilterSupport method filterUsingWildcard.

@Test
public void filterUsingWildcard() throws Exception {
    TestAppender ta1 = registerAppender("filterUsingWildcard1", "app1");
    TestAppender ta2 = registerAppender("filterUsingWildcard2", "app2");
    //Set additivity to false to prevent other appender like CONSOLE from
    //interfering
    org.slf4j.Logger baz1 = LoggerFactory.getLogger("filterUsingWildcard1.foo.baz");
    ((ch.qos.logback.classic.Logger) baz1).setAdditive(false);
    org.slf4j.Logger baz2 = LoggerFactory.getLogger("filterUsingWildcard2.foo.baz");
    ((ch.qos.logback.classic.Logger) baz2).setAdditive(false);
    final List<String> msgs = new ArrayList<String>();
    Filter stf = new Filter<ILoggingEvent>() {

        @Override
        public FilterReply decide(ILoggingEvent event) {
            msgs.add(event.getMessage());
            return FilterReply.NEUTRAL;
        }
    };
    Dictionary<String, Object> props = new Hashtable<String, Object>();
    props.put("appenders", "*");
    ServiceRegistration sr = bundleContext.registerService(Filter.class.getName(), stf, props);
    delay();
    baz1.info("baz1-1");
    baz2.info("baz2-1");
    assertEquals(2, msgs.size());
}
Also used : Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) MatchingFilter(ch.qos.logback.classic.turbo.MatchingFilter) TurboFilter(ch.qos.logback.classic.turbo.TurboFilter) Filter(ch.qos.logback.core.filter.Filter) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 2 with Filter

use of ch.qos.logback.core.filter.Filter in project sling by apache.

the class ITFilterSupport method testNormalFilter.

@Test
public void testNormalFilter() {
    TestAppender ta = registerAppender("filter", "TestAppender");
    org.slf4j.Logger bar = LoggerFactory.getLogger("filter.foo.bar");
    assertTrue(bar.isDebugEnabled());
    bar.debug("Test");
    assertEquals(1, ta.events.size());
    SimpleFilter stf = new SimpleFilter();
    Dictionary<String, Object> props = new Hashtable<String, Object>();
    props.put("appenders", "TestAppender");
    ServiceRegistration sr = bundleContext.registerService(Filter.class.getName(), stf, props);
    delay();
    assertNotNull("Filter should have context set", stf.getContext());
    assertTrue("Filter should be started", stf.isStarted());
    ta.events.clear();
    //A filter attached to an appender cannot influence isXXXEnabled call
    assertTrue(bar.isDebugEnabled());
    //No events should be logged as filter would have rejected that
    bar.debug("Test");
    assertTrue(ta.events.isEmpty());
    //Now unregister and earlier asserts should work
    sr.unregister();
    delay();
    ta.events.clear();
    assertTrue(bar.isDebugEnabled());
    bar.debug("Test");
    assertEquals(1, ta.events.size());
}
Also used : MatchingFilter(ch.qos.logback.classic.turbo.MatchingFilter) TurboFilter(ch.qos.logback.classic.turbo.TurboFilter) Filter(ch.qos.logback.core.filter.Filter) Hashtable(java.util.Hashtable) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Aggregations

MatchingFilter (ch.qos.logback.classic.turbo.MatchingFilter)2 TurboFilter (ch.qos.logback.classic.turbo.TurboFilter)2 Filter (ch.qos.logback.core.filter.Filter)2 Hashtable (java.util.Hashtable)2 Test (org.junit.Test)2 ServiceRegistration (org.osgi.framework.ServiceRegistration)2 Logger (ch.qos.logback.classic.Logger)1 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)1 ArrayList (java.util.ArrayList)1