Search in sources :

Example 6 with TurboFilter

use of ch.qos.logback.classic.turbo.TurboFilter in project sling by apache.

the class LogTracerTest method checkTracing.

@Test
public void checkTracing() throws Exception {
    HttpServletRequest request = mock(SlingHttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(request.getParameter(LogTracer.PARAM_TRACER_CONFIG)).thenReturn("a.b.c;level=trace,a.b;level=debug");
    activateTracer();
    Level oldLevel = rootLogger().getLevel();
    rootLogger().setLevel(Level.INFO);
    FilterChain chain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            TurboFilter turboFilter = context.getService(TurboFilter.class);
            assertNotNull(turboFilter);
            getLogContext().addTurboFilter(turboFilter);
            getLogger("a").info("a-info");
            getLogger("a").debug("a-debug");
            getLogger("a.b").info("a.b-info");
            getLogger("a.b").debug("a.b-debug");
            getLogger("a.b").trace("a.b-trace");
            getLogger("a.b.c").debug("a.b.c-debug");
            getLogger("a.b.c").trace("a.b.c-trace");
            getLogger("a.b.c.d").trace("a.b.c.d-trace");
            if (getLogger("a.b.c").isTraceEnabled()) {
                getLogger("a.b.c").trace("a.b.c-trace2");
            }
        }
    };
    Filter filter = getFilter(true);
    filter.doFilter(request, response, chain);
    assertNull(context.getService(TurboFilter.class));
    List<String> logs = logCollector.getLogs();
    assertThat(logs, hasItem("a-info"));
    assertThat(logs, not(hasItem("a-debug")));
    assertThat(logs, hasItem("a.b-info"));
    assertThat(logs, hasItem("a.b-debug"));
    assertThat(logs, not(hasItem("a.b-trace")));
    assertThat(logs, hasItem("a.b.c-debug"));
    assertThat(logs, hasItem("a.b.c-trace"));
    assertThat(logs, hasItem("a.b.c-trace2"));
    assertThat(logs, hasItem("a.b.c.d-trace"));
    rootLogger().setLevel(oldLevel);
}
Also used : SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) TurboFilter(ch.qos.logback.classic.turbo.TurboFilter) Filter(javax.servlet.Filter) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) Level(ch.qos.logback.classic.Level) TurboFilter(ch.qos.logback.classic.turbo.TurboFilter) Test(org.junit.Test)

Example 7 with TurboFilter

use of ch.qos.logback.classic.turbo.TurboFilter in project sling by apache.

the class LogTracerTest method turboFilterRegisteredWhenTracingRequested.

@Test
public void turboFilterRegisteredWhenTracingRequested() throws Exception {
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(request.getHeader(LogTracer.HEADER_TRACER_CONFIG)).thenReturn("foo.bar");
    activateTracer();
    FilterChain chain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            assertNotNull(context.getService(TurboFilter.class));
        }
    };
    Filter filter = getFilter(false);
    filter.doFilter(request, response, chain);
    assertNull("TurboFilter should get removed once request is done", context.getService(TurboFilter.class));
}
Also used : SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) TurboFilter(ch.qos.logback.classic.turbo.TurboFilter) Filter(javax.servlet.Filter) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) TurboFilter(ch.qos.logback.classic.turbo.TurboFilter) Test(org.junit.Test)

Example 8 with TurboFilter

use of ch.qos.logback.classic.turbo.TurboFilter in project sling by apache.

the class LogTracerTest method recordingWithoutTracing.

@Test
public void recordingWithoutTracing() throws Exception {
    activateTracerAndServlet();
    MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(context.bundleContext()) {

        @Override
        public RequestProgressTracker getRequestProgressTracker() {
            return createTracker("x", "y");
        }

        @Override
        public String getRequestURI() {
            return "foo";
        }
    };
    request.setHeader(TracerLogServlet.HEADER_TRACER_RECORDING, "true");
    HttpServletResponse response = mock(HttpServletResponse.class);
    FilterChain chain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            //No TurboFilter should be registered if tracing is not requested
            assertNull(context.getService(TurboFilter.class));
        }
    };
    prepareChain(chain).doFilter(request, response);
    String requestId = getRequestId(response);
    assertNotNull(requestId);
    Recording r = ((TracerLogServlet) context.getService(Servlet.class)).getRecording(requestId);
    assertTrue(r instanceof JSONRecording);
    JSONRecording jr = (JSONRecording) r;
    StringWriter sw = new StringWriter();
    jr.render(sw);
    JsonObject json = Json.createReader(new StringReader(sw.toString())).readObject();
    assertEquals(2, json.getJsonArray("requestProgressLogs").size());
}
Also used : SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) JsonObject(javax.json.JsonObject) StringWriter(java.io.StringWriter) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) StringReader(java.io.StringReader) Servlet(javax.servlet.Servlet) TurboFilter(ch.qos.logback.classic.turbo.TurboFilter) Test(org.junit.Test)

Example 9 with TurboFilter

use of ch.qos.logback.classic.turbo.TurboFilter in project sling by apache.

the class LogTracerTest method turboFilterRegisteredWhenTracingRequested_Sling.

@Test
public void turboFilterRegisteredWhenTracingRequested_Sling() throws Exception {
    HttpServletRequest request = mock(SlingHttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(request.getParameter(LogTracer.PARAM_TRACER_CONFIG)).thenReturn("foo.bar");
    activateTracer();
    FilterChain chain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            assertNotNull(context.getService(TurboFilter.class));
        }
    };
    Filter filter = getFilter(true);
    filter.doFilter(request, response, chain);
    assertNull("TurboFilter should get removed once request is done", context.getService(TurboFilter.class));
}
Also used : SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) TurboFilter(ch.qos.logback.classic.turbo.TurboFilter) Filter(javax.servlet.Filter) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) TurboFilter(ch.qos.logback.classic.turbo.TurboFilter) Test(org.junit.Test)

Example 10 with TurboFilter

use of ch.qos.logback.classic.turbo.TurboFilter in project sling by apache.

the class ITFilterSupport method testTurboFilter.

@Test
public void testTurboFilter() throws Exception {
    TestAppender ta = registerAppender("turbofilter", "TestAppender");
    org.slf4j.Logger bar = LoggerFactory.getLogger("turbofilter.foo.bar");
    assertTrue(bar.isDebugEnabled());
    bar.debug("Test");
    assertEquals(1, ta.events.size());
    SimpleTurboFilter stf = new SimpleTurboFilter();
    ServiceRegistration sr = bundleContext.registerService(TurboFilter.class.getName(), stf, null);
    delay();
    assertNotNull("Filter should have context set", stf.getContext());
    assertTrue("Filter should be started", stf.isStarted());
    ta.events.clear();
    //Filter would reject calls for this logger hence it should not be false
    assertFalse(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 : ServiceRegistration(org.osgi.framework.ServiceRegistration) TurboFilter(ch.qos.logback.classic.turbo.TurboFilter) Test(org.junit.Test)

Aggregations

TurboFilter (ch.qos.logback.classic.turbo.TurboFilter)10 Test (org.junit.Test)5 FilterChain (javax.servlet.FilterChain)4 ServletRequest (javax.servlet.ServletRequest)4 ServletResponse (javax.servlet.ServletResponse)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)4 MockSlingHttpServletRequest (org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest)4 Filter (javax.servlet.Filter)3 LoggerContext (ch.qos.logback.classic.LoggerContext)2 TurboFilterList (ch.qos.logback.classic.spi.TurboFilterList)2 ReconfigureOnChangeFilter (ch.qos.logback.classic.turbo.ReconfigureOnChangeFilter)2 Logger (org.slf4j.Logger)2 Level (ch.qos.logback.classic.Level)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 JsonObject (javax.json.JsonObject)1 Servlet (javax.servlet.Servlet)1 ServiceRegistration (org.osgi.framework.ServiceRegistration)1