Skip to content

Commit bfba988

Browse files
committed
AbstractRequestLoggingFilter allows for dedicated shouldLog check in CommonsRequestLoggingFilter
Issue: SPR-12609 (cherry picked from commit d4dac25)
1 parent a7975c6 commit bfba988

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

spring-web/src/main/java/org/springframework/web/filter/AbstractRequestLoggingFilter.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -211,14 +211,15 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
211211
requestToUse = new ContentCachingRequestWrapper(request);
212212
}
213213

214-
if (isFirstRequest) {
214+
boolean shouldLog = shouldLog(requestToUse);
215+
if (shouldLog && isFirstRequest) {
215216
beforeRequest(requestToUse, getBeforeMessage(requestToUse));
216217
}
217218
try {
218219
filterChain.doFilter(requestToUse, response);
219220
}
220221
finally {
221-
if (!isAsyncStarted(requestToUse)) {
222+
if (shouldLog && !isAsyncStarted(requestToUse)) {
222223
afterRequest(requestToUse, getAfterMessage(requestToUse));
223224
}
224225
}
@@ -289,6 +290,22 @@ protected String createMessage(HttpServletRequest request, String prefix, String
289290
return msg.toString();
290291
}
291292

293+
294+
/**
295+
* Determine whether to call the {@link #beforeRequest}/{@link #afterRequest}
296+
* methods for the current request, i.e. whether logging is currently active
297+
* (and the log message is worth building).
298+
* <p>The default implementation always returns {@code true}. Subclasses may
299+
* override this with a log level check.
300+
* @param request current HTTP request
301+
* @return {@code true} if the before/after method should get called;
302+
* {@code false} otherwise
303+
* @since 4.1.5
304+
*/
305+
protected boolean shouldLog(HttpServletRequest request) {
306+
return true;
307+
}
308+
292309
/**
293310
* Concrete subclasses should implement this method to write a log message
294311
* <i>before</i> the request is processed.

spring-web/src/main/java/org/springframework/web/filter/CommonsRequestLoggingFilter.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2005 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323
* (and optionally the query string) to the Commons Log.
2424
*
2525
* @author Rob Harrop
26+
* @author Juergen Hoeller
2627
* @since 1.2.5
2728
* @see #setIncludeQueryString
2829
* @see #setBeforeMessagePrefix
@@ -33,24 +34,25 @@
3334
*/
3435
public class CommonsRequestLoggingFilter extends AbstractRequestLoggingFilter {
3536

37+
@Override
38+
protected boolean shouldLog(HttpServletRequest request) {
39+
return logger.isDebugEnabled();
40+
}
41+
3642
/**
3743
* Writes a log message before the request is processed.
3844
*/
3945
@Override
4046
protected void beforeRequest(HttpServletRequest request, String message) {
41-
if (logger.isDebugEnabled()) {
42-
logger.debug(message);
43-
}
47+
logger.debug(message);
4448
}
4549

4650
/**
4751
* Writes a log message after the request is processed.
4852
*/
4953
@Override
5054
protected void afterRequest(HttpServletRequest request, String message) {
51-
if (logger.isDebugEnabled()) {
52-
logger.debug(message);
53-
}
55+
logger.debug(message);
5456
}
5557

5658
}

0 commit comments

Comments
 (0)