Make server request log pluggable.
@@ -28,6 +28,7 @@ import org.eclipse.jetty.security.SecurityHandler;
import org.eclipse.jetty.security.authentication.BasicAuthenticator;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.NetworkTrafficServerConnector;
+import org.eclipse.jetty.server.RequestLog;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerCollection;
@@ -311,10 +312,7 @@ public abstract class Application<T extends RestConfig> {
context.addServlet(defaultHolder, "/*");
RequestLogHandler requestLogHandler = new RequestLogHandler();
- Slf4jRequestLog requestLog = new Slf4jRequestLog();
- requestLog.setLoggerName(config.getString(RestConfig.REQUEST_LOGGER_NAME_CONFIG));
- requestLog.setLogLatency(true);
- requestLogHandler.setRequestLog(requestLog);
+ requestLogHandler.setRequestLog(getRequestLog());
HandlerCollection handlers = new HandlerCollection();
handlers.setHandlers(new Handler[]{context, new DefaultHandler(), requestLogHandler});
@@ -514,6 +512,29 @@ public abstract class Application<T extends RestConfig> {
return new ObjectMapper();
}
+ /**
+ * Returns the default {@link Slf4jRequestLog} used. Separate method made available in order to
+ * allow overloading of {@link #getRequestLog()} by returning a different class of
+ * {@link RequestLog} without making it impossible to rely on the class of the default
+ * implementation being an instance of {@link Slf4jRequestLog}.
+ */
+ protected final Slf4jRequestLog getSlf4jRequestLog() {
+ Slf4jRequestLog result = new Slf4jRequestLog();
+ result.setLoggerName(config.getString(RestConfig.REQUEST_LOGGER_NAME_CONFIG));
+ result.setLogLatency(true);
+ return result;
+ }
+
+ /**
+ * Gets a {@link RequestLog} to use for logging requests. By default, returns
+ * {@link #getSlf4jRequestLog()}. One simple example of use would be to set the
+ * {@link Slf4jRequestLog#setExtended(boolean)} method of the log in order to enable extended
+ * logging (http referer field, user agent) information.
+ */
+ protected RequestLog getRequestLog() {
+ return getSlf4jRequestLog();
+ }
+
/**
* Start the server (creating it if necessary).
* @throws Exception If the application fails to start
