Skip to content

Commit

Permalink
P4ADEV-1781 improve ControllerExceptionHandler logging
Browse files Browse the repository at this point in the history
  • Loading branch information
serdimic committed Dec 20, 2024
1 parent b443fa6 commit 1e77889
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,37 @@

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.event.Level;
import org.springdoc.api.ErrorMessage;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class ControllerExceptionHandler {

@ExceptionHandler(ResourceNotFoundException.class)
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public ErrorMessage resourceNotFoundException(ResourceNotFoundException ex, HttpServletRequest request) {
if(log.isInfoEnabled()) {
String logMessage = "A ResourceNotFoundException occurred handling request %s - HttpStatus %s - %s"
.formatted(getRequestDetails(request), HttpStatus.NOT_FOUND.value(), ex.getMessage());
if(log.isDebugEnabled())
log.debug(logMessage, ex);
else
log.info(logMessage);
}
return new ErrorMessage("resource not found: %s".formatted(ex.getMessage()));
public ResponseEntity<ErrorMessage> resourceNotFoundException(ResourceNotFoundException ex, HttpServletRequest request) {
HttpStatus returnStatus = HttpStatus.NOT_FOUND;
logException(ex, request, returnStatus, Level.INFO, false);
return ResponseEntity.status(returnStatus)
.body(new ErrorMessage("resource not found: %s".formatted(ex.getMessage())));
}

private String getRequestDetails(HttpServletRequest request) {
return "%s %s".formatted(request.getMethod(), request.getRequestURI());
private void logException(Exception ex, HttpServletRequest request, HttpStatus httpStatus, Level level, boolean printStackTrace) {
printStackTrace = printStackTrace | log.isTraceEnabled();
log.atLevel(level)
.setCause(printStackTrace ? ex : null)
.log("A {} occurred handling request {} {} - HttpStatus {} - {}",
ex.getClass().getSimpleName(),
request.getMethod(),
request.getRequestURI(),
httpStatus.value(),
ex.getMessage()
);
}

}
44 changes: 24 additions & 20 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
spring:
application:
name: ${artifactId}
version: ${version}
jmx.enabled: true
datasource:
url: \${ORGANIZATION_DB_URL:jdbc:postgresql://\${ORGANIZATION_DB_HOST:localhost}:\${ORGANIZATION_DB_PORT:5432}/\${ORGANIZATION_DB_NAME:mypay}}
username: \${ORGANIZATION_DB_USER}
password: \${ORGANIZATION_DB_PASSWORD}
driverClassName: org.postgresql.Driver
application:
name: ${artifactId}
version: ${version}
jmx.enabled: true
datasource:
url: \${ORGANIZATION_DB_URL:jdbc:postgresql://\${ORGANIZATION_DB_HOST:localhost}:\${ORGANIZATION_DB_PORT:5432}/\${ORGANIZATION_DB_NAME:mypay}}
username: \${ORGANIZATION_DB_USER}
password: \${ORGANIZATION_DB_PASSWORD}
driverClassName: org.postgresql.Driver

management:
endpoint:
health:
probes.enabled: true
group:
readiness.include: "*"
liveness.include: livenessState,diskSpace,ping
endpoints:
jmx:
exposure.include: "*"
web:
exposure.include: info, health
endpoint:
health:
probes.enabled: true
group:
readiness.include: "*"
liveness.include: livenessState,diskSpace,ping
endpoints:
jmx:
exposure.include: "*"
web:
exposure.include: info, health

logging:
level:
it.gov.pagopa.pu.organization.exception.ControllerExceptionHandler: "\${LOGGING_LEVEL_CONTROLLER_EXCEPTION:INFO}"

app:
brokerEncryptPassword: \${BROKER_ENCRYPT_PASSWORD}

0 comments on commit 1e77889

Please sign in to comment.