6161public class DSpaceApiExceptionControllerAdvice extends ResponseEntityExceptionHandler {
6262 private static final Logger log = LogManager .getLogger ();
6363
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+
6472 /**
6573 * Default collection of HTTP error codes to log as ERROR with full stack trace.
6674 */
@@ -290,7 +298,6 @@ private void sendErrorResponse(final HttpServletRequest request,
290298 // Log the full error and status code
291299 log .error ("{} (status:{})" , message , statusCode , ex );
292300 } else if (HttpStatus .valueOf (statusCode ).is4xxClientError ()) {
293- // Log the error as a single-line WARN
294301 String location ;
295302 String exceptionMessage ;
296303 if (null == ex ) {
@@ -301,12 +308,26 @@ private void sendErrorResponse(final HttpServletRequest request,
301308 StackTraceElement [] trace = ex .getStackTrace ();
302309 location = trace .length <= 0 ? "unknown" : trace [0 ].toString ();
303310 }
304- log .warn ("{} (status:{} exception: {} at: {})" , message , statusCode ,
305- exceptionMessage , location );
311+ logClientError (statusCode , message , exceptionMessage , location );
306312 }
307313
308314 //Exception properties will be set by org.springframework.boot.web.support.ErrorPageFilter
309315 response .sendError (statusCode , message );
310316 }
311317
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+
312333}
0 commit comments