|
1 | 1 | /* |
2 | | - * Copyright 2002-2014 the original author or authors. |
| 2 | + * Copyright 2002-2015 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
@@ -211,14 +211,15 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse |
211 | 211 | requestToUse = new ContentCachingRequestWrapper(request); |
212 | 212 | } |
213 | 213 |
|
214 | | - if (isFirstRequest) { |
| 214 | + boolean shouldLog = shouldLog(requestToUse); |
| 215 | + if (shouldLog && isFirstRequest) { |
215 | 216 | beforeRequest(requestToUse, getBeforeMessage(requestToUse)); |
216 | 217 | } |
217 | 218 | try { |
218 | 219 | filterChain.doFilter(requestToUse, response); |
219 | 220 | } |
220 | 221 | finally { |
221 | | - if (!isAsyncStarted(requestToUse)) { |
| 222 | + if (shouldLog && !isAsyncStarted(requestToUse)) { |
222 | 223 | afterRequest(requestToUse, getAfterMessage(requestToUse)); |
223 | 224 | } |
224 | 225 | } |
@@ -289,6 +290,22 @@ protected String createMessage(HttpServletRequest request, String prefix, String |
289 | 290 | return msg.toString(); |
290 | 291 | } |
291 | 292 |
|
| 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 | + |
292 | 309 | /** |
293 | 310 | * Concrete subclasses should implement this method to write a log message |
294 | 311 | * <i>before</i> the request is processed. |
|
0 commit comments