Skip to content

Commit 172cc68

Browse files
committed
Reduce warn logs noise (#1268) + line length fix (#1274)
1 parent c28d601 commit 172cc68

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
*/
@@ -300,12 +308,25 @@ private void sendErrorResponse(final HttpServletRequest request,
300308
StackTraceElement[] trace = ex.getStackTrace();
301309
location = trace.length <= 0 ? "unknown" : trace[0].toString();
302310
}
303-
log.warn("{} (status:{} exception: {} at: {})", message, statusCode,
304-
exceptionMessage, location);
305-
}
311+
logClientError(statusCode, message,
306312

307313
//Exception properties will be set by org.springframework.boot.web.support.ErrorPageFilter
308314
response.sendError(statusCode, message);
309315
}
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+

dspace/config/log4j2.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@
6666
<AppenderRef ref='A2'/>
6767
</Logger>
6868

69+
<!-- NotFound logger for 404 responses - set to WARN to enable 404 logging -->
70+
<Logger name='org.dspace.app.rest.exception.DSpaceApiExceptionControllerAdvice.NotFound'
71+
level='OFF'
72+
additivity='false'>
73+
<AppenderRef ref='A1'/>
74+
</Logger>
75+
6976
<!-- Block services logging except on exceptions -->
7077
<Logger name='org.dspace.kernel'
7178
level='ERROR'/>

0 commit comments

Comments
 (0)