Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@
public class DSpaceApiExceptionControllerAdvice extends ResponseEntityExceptionHandler {
private static final Logger log = LogManager.getLogger();

/**
* Dedicated logger for 404 NOT_FOUND responses. Configured at OFF level by default
* so that expected 404s don't flood production logs.
* Set to WARN in log4j2.xml to see 404 responses in logs.
*/
private static final Logger notFoundLog =
LogManager.getLogger("org.dspace.app.rest.exception.DSpaceApiExceptionControllerAdvice.NotFound");

/**
* Default collection of HTTP error codes to log as ERROR with full stack trace.
*/
Expand Down Expand Up @@ -290,7 +298,6 @@ private void sendErrorResponse(final HttpServletRequest request,
// Log the full error and status code
log.error("{} (status:{})", message, statusCode, ex);
} else if (HttpStatus.valueOf(statusCode).is4xxClientError()) {
// Log the error as a single-line WARN
String location;
String exceptionMessage;
if (null == ex) {
Expand All @@ -301,12 +308,26 @@ private void sendErrorResponse(final HttpServletRequest request,
StackTraceElement[] trace = ex.getStackTrace();
location = trace.length <= 0 ? "unknown" : trace[0].toString();
}
log.warn("{} (status:{} exception: {} at: {})", message, statusCode,
exceptionMessage, location);
logClientError(statusCode, message, exceptionMessage, location);
}

//Exception properties will be set by org.springframework.boot.web.support.ErrorPageFilter
response.sendError(statusCode, message);
}

/**
* Log a 4xx client error. 404 NOT_FOUND is sent to a dedicated logger ({@link #notFoundLog})
* at WARN level, but the logger is set to OFF by default in log4j2.xml (suppressed).
* Set logger to WARN in log4j2.xml to see 404 responses in logs.
*/
private void logClientError(int statusCode, String message, String exceptionMessage, String location) {
if (statusCode == HttpServletResponse.SC_NOT_FOUND) {
notFoundLog.warn("{} (status:{} exception: {} at: {})", message, statusCode,
exceptionMessage, location);
} else {
log.warn("{} (status:{} exception: {} at: {})", message, statusCode,
exceptionMessage, location);
}
}

}
7 changes: 7 additions & 0 deletions dspace/config/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@
<AppenderRef ref='A2'/>
</Logger>

<!-- NotFound logger for 404 responses - set to WARN to enable 404 logging -->
<Logger name='org.dspace.app.rest.exception.DSpaceApiExceptionControllerAdvice.NotFound'
level='OFF'
additivity='false'>
<AppenderRef ref='A1'/>
</Logger>

<!-- The file downloads -->
<Logger name="org.dspace.app.statistics.clarin.ClarinMatomoBitstreamTracker"
level="INFO"
Expand Down
Loading