Skip to content

Commit 461cc12

Browse files
committed
Reduce warn logs noise (#1268) + line length fix (#1274)
1 parent 0039733 commit 461cc12

2 files changed

Lines changed: 31 additions & 3 deletions

File tree

dspace-server-webapp/src/main/java/org/dspace/app/rest/exception/DSpaceApiExceptionControllerAdvice.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@
6161
public 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
}

dspace/config/log4j2.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@
9595
<AppenderRef ref='A2'/>
9696
</Logger>
9797

98+
<!-- NotFound logger for 404 responses - set to WARN to enable 404 logging -->
99+
<Logger name='org.dspace.app.rest.exception.DSpaceApiExceptionControllerAdvice.NotFound'
100+
level='OFF'
101+
additivity='false'>
102+
<AppenderRef ref='A1'/>
103+
</Logger>
104+
98105
<!-- The file downloads -->
99106
<Logger name="org.dspace.app.statistics.clarin.ClarinMatomoBitstreamTracker"
100107
level="INFO"

0 commit comments

Comments
 (0)