diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/CustomRequestLog.java b/jetty-server/src/main/java/org/eclipse/jetty/server/CustomRequestLog.java index 33db3e00871..a0da252bff3 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/CustomRequestLog.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/CustomRequestLog.java @@ -22,6 +22,7 @@ import java.util.List; import java.util.Locale; import java.util.TimeZone; import java.util.concurrent.TimeUnit; +import java.util.function.BiPredicate; import java.util.function.Supplier; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -279,6 +280,7 @@ public class CustomRequestLog extends ContainerLifeCycle implements RequestLog private final String _formatString; private transient PathMappings _ignorePathMap; private String[] _ignorePaths; + private BiPredicate _filter; public CustomRequestLog() { @@ -311,6 +313,15 @@ public class CustomRequestLog extends ContainerLifeCycle implements RequestLog } } + /** + * This allows you to set a custom filter to decide whether to log a request or omit it from the request log. + * @param filter - a BiPredicate which returns true if this request should be logged. + */ + public void setFilter(BiPredicate filter) + { + _filter = filter; + } + @ManagedAttribute("The RequestLogWriter") public RequestLog.Writer getWriter() { @@ -325,11 +336,14 @@ public class CustomRequestLog extends ContainerLifeCycle implements RequestLog @Override public void log(Request request, Response response) { + if (_ignorePathMap != null && _ignorePathMap.getMatch(request.getRequestURI()) != null) + return; + + if (_filter != null && !_filter.test(request, response)) + return; + try { - if (_ignorePathMap != null && _ignorePathMap.getMatch(request.getRequestURI()) != null) - return; - StringBuilder sb = _buffers.get(); sb.setLength(0); diff --git a/tests/test-integration/src/test/java/org/eclipse/jetty/test/CustomRequestLogTest.java b/tests/test-integration/src/test/java/org/eclipse/jetty/test/CustomRequestLogTest.java index 41a04b19882..fe6fe40727d 100644 --- a/tests/test-integration/src/test/java/org/eclipse/jetty/test/CustomRequestLogTest.java +++ b/tests/test-integration/src/test/java/org/eclipse/jetty/test/CustomRequestLogTest.java @@ -27,7 +27,7 @@ import java.util.Locale; import java.util.Objects; import java.util.concurrent.BlockingQueue; import java.util.concurrent.TimeUnit; -import javax.servlet.ServletException; +import java.util.concurrent.atomic.AtomicReference; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; @@ -142,6 +142,22 @@ public class CustomRequestLogTest _server.stop(); } + @Test + public void testRequestFilter() throws Exception + { + AtomicReference logRequest = new AtomicReference<>(); + testHandlerServerStart("RequestPath: %U"); + _log.setFilter((request, response) -> logRequest.get()); + + logRequest.set(true); + _connector.getResponse("GET /path HTTP/1.0\n\n"); + assertThat(_entries.poll(5, TimeUnit.SECONDS), is("RequestPath: /path")); + + logRequest.set(false); + _connector.getResponse("GET /path HTTP/1.0\n\n"); + assertNull(_entries.poll(1, TimeUnit.SECONDS)); + } + @Test public void testLogRemoteUser() throws Exception {