AbstractRequestLoggingFilter allows for dedicated shouldLog check in CommonsRequestLoggingFilter

Issue: SPR-12609
This commit is contained in:
Juergen Hoeller 2015-01-20 16:01:53 +01:00
parent 0ddf8dde12
commit d4dac250a8
2 changed files with 29 additions and 10 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -211,14 +211,15 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
requestToUse = new ContentCachingRequestWrapper(request);
}
if (isFirstRequest) {
boolean shouldLog = shouldLog(requestToUse);
if (shouldLog && isFirstRequest) {
beforeRequest(requestToUse, getBeforeMessage(requestToUse));
}
try {
filterChain.doFilter(requestToUse, response);
}
finally {
if (!isAsyncStarted(requestToUse)) {
if (shouldLog && !isAsyncStarted(requestToUse)) {
afterRequest(requestToUse, getAfterMessage(requestToUse));
}
}
@ -289,6 +290,22 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
return msg.toString();
}
/**
* Determine whether to call the {@link #beforeRequest}/{@link #afterRequest}
* methods for the current request, i.e. whether logging is currently active
* (and the log message is worth building).
* <p>The default implementation always returns {@code true}. Subclasses may
* override this with a log level check.
* @param request current HTTP request
* @return {@code true} if the before/after method should get called;
* {@code false} otherwise
* @since 4.1.5
*/
protected boolean shouldLog(HttpServletRequest request) {
return true;
}
/**
* Concrete subclasses should implement this method to write a log message
* <i>before</i> the request is processed.

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2005 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -23,6 +23,7 @@ import javax.servlet.http.HttpServletRequest;
* (and optionally the query string) to the Commons Log.
*
* @author Rob Harrop
* @author Juergen Hoeller
* @since 1.2.5
* @see #setIncludeQueryString
* @see #setBeforeMessagePrefix
@ -33,14 +34,17 @@ import javax.servlet.http.HttpServletRequest;
*/
public class CommonsRequestLoggingFilter extends AbstractRequestLoggingFilter {
@Override
protected boolean shouldLog(HttpServletRequest request) {
return logger.isDebugEnabled();
}
/**
* Writes a log message before the request is processed.
*/
@Override
protected void beforeRequest(HttpServletRequest request, String message) {
if (logger.isDebugEnabled()) {
logger.debug(message);
}
logger.debug(message);
}
/**
@ -48,9 +52,7 @@ public class CommonsRequestLoggingFilter extends AbstractRequestLoggingFilter {
*/
@Override
protected void afterRequest(HttpServletRequest request, String message) {
if (logger.isDebugEnabled()) {
logger.debug(message);
}
logger.debug(message);
}
}