|
61 | 61 | public class DSpaceApiExceptionControllerAdvice extends ResponseEntityExceptionHandler { |
62 | 62 | private static final Logger log = LogManager.getLogger(); |
63 | 63 |
|
| 64 | + /** |
| 65 | + * Dedicated logger for 404 NOT_FOUND responses. Configured at OFF level by default |
| 66 | + * so that expected 404s don't flood production logs. |
| 67 | + * Set to WARN in log4j2.xml to see 404 responses in logs. |
| 68 | + */ |
| 69 | + private static final Logger notFoundLog = |
| 70 | + LogManager.getLogger("org.dspace.app.rest.exception.DSpaceApiExceptionControllerAdvice.NotFound"); |
| 71 | + |
64 | 72 | /** |
65 | 73 | * Default collection of HTTP error codes to log as ERROR with full stack trace. |
66 | 74 | */ |
@@ -300,12 +308,25 @@ private void sendErrorResponse(final HttpServletRequest request, |
300 | 308 | StackTraceElement[] trace = ex.getStackTrace(); |
301 | 309 | location = trace.length <= 0 ? "unknown" : trace[0].toString(); |
302 | 310 | } |
303 | | - log.warn("{} (status:{} exception: {} at: {})", message, statusCode, |
304 | | - exceptionMessage, location); |
305 | | - } |
| 311 | + logClientError(statusCode, message, |
306 | 312 |
|
307 | 313 | //Exception properties will be set by org.springframework.boot.web.support.ErrorPageFilter |
308 | 314 | response.sendError(statusCode, message); |
309 | 315 | } |
310 | 316 |
|
311 | 317 | } |
| 318 | + /** |
| 319 | + * Log a 4xx client error. 404 NOT_FOUND is sent to a dedicated logger ({@link #notFoundLog}) |
| 320 | + * at WARN level, but the logger is set to OFF by default in log4j2.xml (suppressed). |
| 321 | + * Set logger to WARN in log4j2.xml to see 404 responses in logs. |
| 322 | + */ |
| 323 | + private void logClientError(int statusCode, String message, String exceptionMessage, String location) { |
| 324 | + if (statusCode == HttpServletResponse.SC_NOT_FOUND) { |
| 325 | + notFoundLog.warn("{} (status:{} exception: {} at: {})", message, statusCode, |
| 326 | + exceptionMessage, location); |
| 327 | + } else { |
| 328 | + log.warn("{} (status:{} exception: {} at: {})", message, statusCode, |
| 329 | + exceptionMessage, location); |
| 330 | + } |
| 331 | + } |
| 332 | + |
0 commit comments