Issue #5817 - allow CustomRequestLog to be filtered with BiPredicate
Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
parent
8366dcf03e
commit
382aed8374
|
@ -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<String> _ignorePathMap;
|
||||
private String[] _ignorePaths;
|
||||
private BiPredicate<Request, Response> _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<Request, Response> 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);
|
||||
|
||||
|
|
|
@ -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<Boolean> 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
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue