Skip to content

MENDELU/Reduce warn logs noise - 404 dedicated logger#1278

Open
milanmajchrak wants to merge 1 commit intocustomer/mendelufrom
propagate/reduce-warn-logs-v2-mendelu
Open

MENDELU/Reduce warn logs noise - 404 dedicated logger#1278
milanmajchrak wants to merge 1 commit intocustomer/mendelufrom
propagate/reduce-warn-logs-v2-mendelu

Conversation

@milanmajchrak
Copy link
Collaborator

Cherry-pick of commits 1dc5339 and db67645 into customer/mendelu.

Problem description

  • Log 404 responses via dedicated Log4j2 logger (org.dspace.app.rest.exception.DSpaceApiExceptionControllerAdvice.NotFound) set to OFF by default
  • Extract getStatusCodesLoggedAsErrors() and logClientError() helper methods
  • Add sendErrorResponseFromException() for 4xx errors
  • Fix row length to be less than 120 chars

Original commits:

Copilot AI review requested due to automatic review settings March 13, 2026 14:37
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to reduce REST API 404 warning noise by routing NOT_FOUND client errors through a dedicated Log4j2 logger which is OFF by default, while also refactoring parts of the exception logging logic in DSpaceApiExceptionControllerAdvice.

Changes:

  • Add a dedicated Log4j2 logger for 404 NOT_FOUND responses (OFF by default).
  • Refactor status-code-to-log-level handling into helper methods (getStatusCodesLoggedAsErrors(), logClientError()).
  • Introduce sendErrorResponseFromException() helper intended for exposing exception messages in 4xx responses.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
dspace/config/log4j2.xml Adds a dedicated logger entry for 404 NOT_FOUND responses (OFF by default).
dspace-server-webapp/src/main/java/org/dspace/app/rest/exception/DSpaceApiExceptionControllerAdvice.java Adds dedicated 404 logger usage and refactors client-error logging / status-code parsing into helpers.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines 249 to 285
* Send the error to the response. The error message is taken from exception message.
* This method is mainly appropriate for 4xx errors,
* where the exception message should be exposed to REST API clients in response body.
* This method also logs the ERROR log, containing the first line from the exception stack trace.
* Specific errors where an ERROR log with full stack trace is more appropriate are configured
* using property {@code logging.server.include-stacktrace-for-httpcode}
* (see {@link DSpaceApiExceptionControllerAdvice#P_LOG_AS_ERROR}
* and {@link DSpaceApiExceptionControllerAdvice#LOG_AS_ERROR_DEFAULT}).
*
* @param request current request
* @param response current response
* @param ex Exception causing the error response
* @param statusCode status code to send in response
*/
private void sendErrorResponseFromException(final HttpServletRequest request,
final HttpServletResponse response,
final Exception ex,
final int statusCode)
throws IOException {
//Make sure Spring picks up this exception
request.setAttribute(EXCEPTION_ATTRIBUTE, ex);

final Set<Integer> statusCodesLoggedAsErrors = getStatusCodesLoggedAsErrors();

String message = ex.getMessage();
if (statusCodesLoggedAsErrors.contains(statusCode)) {
log.error("{} (status:{})", message, statusCode, ex);
} else {
StackTraceElement[] trace = ex.getStackTrace();
String location = trace.length <= 0 ? "unknown" : trace[0].toString();
logClientError(statusCode, message, ex.getClass().getName(), location);
}

response.sendError(statusCode, message);
}

/**
Comment on lines +249 to +253
* Send the error to the response. The error message is taken from exception message.
* This method is mainly appropriate for 4xx errors,
* where the exception message should be exposed to REST API clients in response body.
* This method also logs the ERROR log, containing the first line from the exception stack trace.
* Specific errors where an ERROR log with full stack trace is more appropriate are configured
Comment on lines +64 to +70
/**
* 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");
Comment on lines +353 to +355
String[] error_codes = configurationService.getArrayProperty(
P_LOG_AS_ERROR, LOG_AS_ERROR_DEFAULT);
for (String code : error_codes) {
Comment on lines +91 to +96
<!-- 404 NOT_FOUND responses. Set to WARN to see 404s in logs. -->
<Logger name='org.dspace.app.rest.exception.DSpaceApiExceptionControllerAdvice.NotFound'
level='OFF'
additivity='false'>
<AppenderRef ref='A1'/>
</Logger>
@milanmajchrak milanmajchrak changed the title Reduce warn logs noise - 404 dedicated logger MENDELU/Reduce warn logs noise - 404 dedicated logger Mar 13, 2026
@milanmajchrak milanmajchrak force-pushed the propagate/reduce-warn-logs-v2-mendelu branch from 33ea0e7 to 4afe794 Compare March 13, 2026 15:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants