From 0bf3494dc2e941e53a495c92310f3847aa11fd09 Mon Sep 17 00:00:00 2001 From: Rajan Dhabalia Date: Mon, 20 Aug 2018 13:47:48 -0700 Subject: [PATCH] add slf4j handler to log web-server log (#2408) --- .../functions/worker/rest/WorkerServer.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/WorkerServer.java b/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/WorkerServer.java index 3e9a4267d3a0e..1c26c7d07824a 100644 --- a/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/WorkerServer.java +++ b/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/WorkerServer.java @@ -21,6 +21,8 @@ import java.util.ArrayList; import java.util.EnumSet; import java.util.List; +import java.util.TimeZone; + import javax.servlet.DispatcherType; import lombok.extern.slf4j.Slf4j; import org.apache.pulsar.broker.web.AuthenticationFilter; @@ -32,6 +34,7 @@ import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; +import org.eclipse.jetty.server.Slf4jRequestLog; import java.net.BindException; import java.net.URI; @@ -39,6 +42,7 @@ import org.eclipse.jetty.server.handler.ContextHandlerCollection; import org.eclipse.jetty.server.handler.DefaultHandler; import org.eclipse.jetty.server.handler.HandlerCollection; +import org.eclipse.jetty.server.handler.RequestLogHandler; import org.eclipse.jetty.servlet.FilterHolder; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; @@ -98,10 +102,19 @@ private void init() { newServletContextHandler("/admin/v2", new ResourceConfig(Resources.getApiResources()), workerService)); handlers.add(newServletContextHandler("/", new ResourceConfig(Resources.getRootResources()), workerService)); + 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() }); + handlerCollection.setHandlers(new Handler[] { contexts, new DefaultHandler(), requestLogHandler }); server.setHandler(handlerCollection); if (this.workerConfig.isTlsEnabled()) {