Search in sources :

Example 31 with RequestLogHandler

use of org.eclipse.jetty.server.handler.RequestLogHandler in project pulsar by yahoo.

the class WebService method start.

public void start() throws PulsarServerException {
    try {
        RequestLogHandler requestLogHandler = new RequestLogHandler();
        Slf4jRequestLog requestLog = new Slf4jRequestLog();
        requestLog.setExtended(true);
        requestLog.setLogTimeZone(WebService.HANDLER_REQUEST_LOG_TZ);
        requestLog.setLogLatency(true);
        requestLogHandler.setRequestLog(requestLog);
        handlers.add(0, new ContextHandlerCollection());
        handlers.add(requestLogHandler);
        ContextHandlerCollection contexts = new ContextHandlerCollection();
        contexts.setHandlers(handlers.toArray(new Handler[handlers.size()]));
        HandlerCollection handlerCollection = new HandlerCollection();
        handlerCollection.setHandlers(new Handler[] { contexts, new DefaultHandler(), requestLogHandler });
        server.setHandler(handlerCollection);
        server.start();
        log.info("Web Service started at {}", pulsar.getWebServiceAddress());
    } catch (Exception e) {
        throw new PulsarServerException(e);
    }
}
Also used : Slf4jRequestLog(org.eclipse.jetty.server.Slf4jRequestLog) PulsarServerException(com.yahoo.pulsar.broker.PulsarServerException) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Handler(org.eclipse.jetty.server.Handler) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) GeneralSecurityException(java.security.GeneralSecurityException) PulsarServerException(com.yahoo.pulsar.broker.PulsarServerException) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler)

Example 32 with RequestLogHandler

use of org.eclipse.jetty.server.handler.RequestLogHandler in project athenz by yahoo.

the class AthenzJettyContainer method addRequestLogHandler.

public void addRequestLogHandler() {
    RequestLogHandler requestLogHandler = new RequestLogHandler();
    // check to see if have a slf4j logger name specified. if we don't
    // then we'll just use our NCSARequestLog extended Athenz logger
    // when using the slf4j logger we don't have the option to pass
    // our audit logger to keep track of unauthenticated requests
    String accessSlf4jLogger = System.getProperty(AthenzConsts.ATHENZ_PROP_ACCESS_SLF4J_LOGGER);
    if (accessSlf4jLogger != null && !accessSlf4jLogger.isEmpty()) {
        Slf4jRequestLog requestLog = new Slf4jRequestLog();
        requestLog.setLoggerName(accessSlf4jLogger);
        requestLog.setExtended(true);
        requestLog.setPreferProxiedForAddress(true);
        requestLog.setLogTimeZone("GMT");
        requestLogHandler.setRequestLog(requestLog);
    } else {
        String logDir = System.getProperty(AthenzConsts.ATHENZ_PROP_ACCESS_LOG_DIR, getRootDir() + "/logs/athenz");
        String logName = System.getProperty(AthenzConsts.ATHENZ_PROP_ACCESS_LOG_NAME, "access.yyyy_MM_dd.log");
        AthenzRequestLog requestLog = new AthenzRequestLog(logDir + File.separator + logName);
        requestLog.setAppend(true);
        requestLog.setExtended(true);
        requestLog.setPreferProxiedForAddress(true);
        requestLog.setLogTimeZone("GMT");
        String retainDays = System.getProperty(AthenzConsts.ATHENZ_PROP_ACCESS_LOG_RETAIN_DAYS, "31");
        int days = Integer.parseInt(retainDays);
        if (days > 0) {
            requestLog.setRetainDays(days);
        }
        requestLogHandler.setRequestLog(requestLog);
    }
    handlers.addHandler(requestLogHandler);
}
Also used : Slf4jRequestLog(org.eclipse.jetty.server.Slf4jRequestLog) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) AthenzRequestLog(com.yahoo.athenz.container.log.AthenzRequestLog)

Example 33 with RequestLogHandler

use of org.eclipse.jetty.server.handler.RequestLogHandler in project athenz by yahoo.

the class AthenzJettyContainerTest method testSlf4jRequestLogHandler.

@Test
public void testSlf4jRequestLogHandler() {
    System.setProperty(AthenzConsts.ATHENZ_PROP_ACCESS_SLF4J_LOGGER, "AthenzAccessLogger");
    AthenzJettyContainer container = new AthenzJettyContainer();
    container.createServer(100);
    container.addRequestLogHandler();
    // now retrieve the request log handler
    Handler[] handlers = container.getHandlers().getHandlers();
    RequestLogHandler logHandler = null;
    for (Handler handler : handlers) {
        if (handler instanceof RequestLogHandler) {
            logHandler = (RequestLogHandler) handler;
            break;
        }
    }
    assertNotNull(logHandler);
    RequestLog reqLog = logHandler.getRequestLog();
    assertNotNull(reqLog);
    assertEquals(reqLog.getClass(), Slf4jRequestLog.class);
    assertEquals(((Slf4jRequestLog) reqLog).getLoggerName(), "AthenzAccessLogger");
    System.clearProperty(AthenzConsts.ATHENZ_PROP_ACCESS_SLF4J_LOGGER);
}
Also used : RequestLog(org.eclipse.jetty.server.RequestLog) AthenzRequestLog(com.yahoo.athenz.container.log.AthenzRequestLog) Slf4jRequestLog(org.eclipse.jetty.server.Slf4jRequestLog) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Handler(org.eclipse.jetty.server.Handler) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) Test(org.testng.annotations.Test)

Example 34 with RequestLogHandler

use of org.eclipse.jetty.server.handler.RequestLogHandler in project javalin by tipsy.

the class TestCustomJetty method test_embeddedServer_withHandlerChain.

@Test
public void test_embeddedServer_withHandlerChain() throws Exception {
    AtomicLong logCount = new AtomicLong(0);
    RequestLog requestLog = (request, response) -> logCount.incrementAndGet();
    RequestLogHandler requestLogHandler = new RequestLogHandler();
    requestLogHandler.setRequestLog(requestLog);
    StatisticsHandler handlerChain = new StatisticsHandler();
    handlerChain.setHandler(requestLogHandler);
    Javalin app = Javalin.create().port(0).embeddedServer(new EmbeddedJettyFactory(() -> {
        Server server = new Server();
        server.setHandler(handlerChain);
        return server;
    })).get("/", ctx -> ctx.result("Hello World")).start();
    String origin = "http://localhost:" + app.port();
    int requests = 10;
    for (int i = 0; i < requests; i++) {
        assertThat(Unirest.get(origin + "/").asString().getBody(), is("Hello World"));
        assertThat(Unirest.get(origin + "/not_there").asString().getStatus(), is(404));
    }
    assertThat(handlerChain.getDispatched(), is(requests * 2));
    assertThat(handlerChain.getResponses2xx(), is(requests));
    assertThat(handlerChain.getResponses4xx(), is(requests));
    assertThat(logCount.get(), is((long) (requests * 2)));
    app.stop();
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) AtomicLong(java.util.concurrent.atomic.AtomicLong) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) Unirest(com.mashape.unirest.http.Unirest) RequestLog(org.eclipse.jetty.server.RequestLog) StatisticsHandler(org.eclipse.jetty.server.handler.StatisticsHandler) EmbeddedJettyFactory(io.javalin.embeddedserver.jetty.EmbeddedJettyFactory) Test(org.junit.Test) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Server(org.eclipse.jetty.server.Server) EmbeddedJettyFactory(io.javalin.embeddedserver.jetty.EmbeddedJettyFactory) AtomicLong(java.util.concurrent.atomic.AtomicLong) RequestLog(org.eclipse.jetty.server.RequestLog) Server(org.eclipse.jetty.server.Server) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) StatisticsHandler(org.eclipse.jetty.server.handler.StatisticsHandler) Test(org.junit.Test)

Example 35 with RequestLogHandler

use of org.eclipse.jetty.server.handler.RequestLogHandler in project incubator-pulsar by apache.

the class ProxyServer method start.

public void start() throws PulsarServerException {
    log.info("Starting web socket proxy at port {}", conf.getWebServicePort());
    try {
        RequestLogHandler requestLogHandler = new RequestLogHandler();
        Slf4jRequestLog requestLog = new Slf4jRequestLog();
        requestLog.setExtended(true);
        requestLog.setLogTimeZone(TimeZone.getDefault().getID());
        requestLog.setLogLatency(true);
        requestLogHandler.setRequestLog(requestLog);
        handlers.add(0, new ContextHandlerCollection());
        handlers.add(requestLogHandler);
        ContextHandlerCollection contexts = new ContextHandlerCollection();
        contexts.setHandlers(handlers.toArray(new Handler[handlers.size()]));
        HandlerCollection handlerCollection = new HandlerCollection();
        handlerCollection.setHandlers(new Handler[] { contexts, new DefaultHandler(), requestLogHandler });
        server.setHandler(handlerCollection);
        server.start();
    } catch (Exception e) {
        throw new PulsarServerException(e);
    }
}
Also used : Slf4jRequestLog(org.eclipse.jetty.server.Slf4jRequestLog) PulsarServerException(org.apache.pulsar.broker.PulsarServerException) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Handler(org.eclipse.jetty.server.Handler) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) ServletException(javax.servlet.ServletException) DeploymentException(javax.websocket.DeploymentException) GeneralSecurityException(java.security.GeneralSecurityException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) MalformedURLException(java.net.MalformedURLException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler)

Aggregations

RequestLogHandler (org.eclipse.jetty.server.handler.RequestLogHandler)45 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)30 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)25 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)22 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)18 Handler (org.eclipse.jetty.server.Handler)14 Slf4jRequestLog (org.eclipse.jetty.server.Slf4jRequestLog)13 Server (org.eclipse.jetty.server.Server)12 ServerConnector (org.eclipse.jetty.server.ServerConnector)12 StatisticsHandler (org.eclipse.jetty.server.handler.StatisticsHandler)10 RequestLog (org.eclipse.jetty.server.RequestLog)8 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)8 File (java.io.File)7 IOException (java.io.IOException)7 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)7 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)7 NCSARequestLog (org.eclipse.jetty.server.NCSARequestLog)6 JacksonJsonProvider (com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider)5 Test (org.junit.Test)5 HttpURLConnection (java.net.HttpURLConnection)4