diff --git a/src/main/java/it/gov/pagopa/apiconfig/selfcareintegration/config/LoggingAspect.java b/src/main/java/it/gov/pagopa/apiconfig/selfcareintegration/config/LoggingAspect.java index 84073618..edb174bf 100644 --- a/src/main/java/it/gov/pagopa/apiconfig/selfcareintegration/config/LoggingAspect.java +++ b/src/main/java/it/gov/pagopa/apiconfig/selfcareintegration/config/LoggingAspect.java @@ -9,8 +9,8 @@ import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; +import org.aspectj.lang.reflect.CodeSignature; import org.slf4j.MDC; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.event.EventListener; @@ -25,9 +25,13 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; import java.util.UUID; import java.util.stream.StreamSupport; +import static it.gov.pagopa.apiconfig.selfcareintegration.util.Utility.deNull; + @Aspect @Component @Slf4j @@ -42,11 +46,10 @@ public class LoggingAspect { public static final String FAULT_DETAIL = "faultDetail"; public static final String REQUEST_ID = "requestId"; public static final String OPERATION_ID = "operationId"; + public static final String ARGS = "args"; - @Autowired - HttpServletRequest httRequest; - @Autowired - HttpServletResponse httpResponse; + final HttpServletRequest httRequest; + final HttpServletResponse httpResponse; @Value("${info.application.artifactId}") private String artifactId; @@ -57,26 +60,9 @@ public class LoggingAspect { @Value("${info.properties.environment}") private String environment; - private static String getExecutionTime() { - String startTime = MDC.get(START_TIME); - if (startTime != null) { - long endTime = System.currentTimeMillis(); - long executionTime = endTime - Long.parseLong(startTime); - return String.valueOf(executionTime); - } - return "1"; - } - - private static String getDetail(ResponseEntity result) { - if (result != null && result.getBody() != null && result.getBody().getDetail() != null) { - return result.getBody().getDetail(); - } else return AppError.UNKNOWN.getDetails(); - } - - private static String getTitle(ResponseEntity result) { - if (result != null && result.getBody() != null && result.getBody().getTitle() != null) { - return result.getBody().getTitle(); - } else return AppError.UNKNOWN.getTitle(); + public LoggingAspect(HttpServletRequest httRequest, HttpServletResponse httpResponse) { + this.httRequest = httRequest; + this.httpResponse = httpResponse; } @Pointcut("@within(org.springframework.web.bind.annotation.RestController)") @@ -142,19 +128,17 @@ public Object logApiInvocation(ProceedingJoinPoint joinPoint) throws Throwable { var requestId = UUID.randomUUID().toString(); MDC.put(REQUEST_ID, requestId); } - log.info("{} {}", httRequest.getMethod(), httRequest.getRequestURI()); - log.info( - "Invoking API operation {} - args: {}", - joinPoint.getSignature().getName(), - joinPoint.getArgs()); + Map params = getParams(joinPoint); + MDC.put(ARGS, params.toString()); + + log.info("Invoking API operation {} - args: {}", joinPoint.getSignature().getName(), params); Object result = joinPoint.proceed(); MDC.put(STATUS, "OK"); MDC.put(CODE, String.valueOf(httpResponse.getStatus())); MDC.put(RESPONSE_TIME, getExecutionTime()); - log.info( - "Successful API operation {} - result: {}", joinPoint.getSignature().getName(), result); + log.info("Successful API operation {} - result: {}", joinPoint.getSignature().getName(), result); MDC.remove(STATUS); MDC.remove(CODE); MDC.remove(RESPONSE_TIME); @@ -163,22 +147,54 @@ public Object logApiInvocation(ProceedingJoinPoint joinPoint) throws Throwable { } @AfterReturning(value = "execution(* *..exception.ErrorHandler.*(..))", returning = "result") - public void trowingApiInvocation(JoinPoint joinPoint, ResponseEntity result) { + public void trowingApiInvocation(JoinPoint joinPoint, ResponseEntity result) { MDC.put(STATUS, "KO"); MDC.put(CODE, String.valueOf(result.getStatusCodeValue())); MDC.put(RESPONSE_TIME, getExecutionTime()); - MDC.put(FAULT_CODE, getTitle((ResponseEntity) result)); - MDC.put(FAULT_DETAIL, getDetail((ResponseEntity) result)); + MDC.put(FAULT_CODE, getTitle(result)); + MDC.put(FAULT_DETAIL, getDetail(result)); log.info("Failed API operation {} - error: {}", MDC.get(METHOD), result); MDC.clear(); } @Around(value = "repository() || service()") public Object logTrace(ProceedingJoinPoint joinPoint) throws Throwable { - log.debug( - "Call method {} - args: {}", joinPoint.getSignature().toShortString(), joinPoint.getArgs()); + Map params = getParams(joinPoint); + log.debug("Call method {} - args: {}", joinPoint.getSignature().toShortString(), params); Object result = joinPoint.proceed(); log.debug("Return method {} - result: {}", joinPoint.getSignature().toShortString(), result); return result; } + + private static String getDetail(ResponseEntity result) { + if(result != null && result.getBody() != null && result.getBody().getDetail() != null) { + return result.getBody().getDetail(); + } else return AppError.UNKNOWN.getDetails(); + } + + private static String getTitle(ResponseEntity result) { + if(result != null && result.getBody() != null && result.getBody().getTitle() != null) { + return result.getBody().getTitle(); + } else return AppError.UNKNOWN.getTitle(); + } + + private static String getExecutionTime() { + String startTime = MDC.get(START_TIME); + if(startTime != null) { + long endTime = System.currentTimeMillis(); + long executionTime = endTime - Long.parseLong(startTime); + return String.valueOf(executionTime); + } + return "-"; + } + + private static Map getParams(ProceedingJoinPoint joinPoint) { + CodeSignature codeSignature = (CodeSignature) joinPoint.getSignature(); + Map params = new HashMap<>(); + int i = 0; + for (var paramName : codeSignature.getParameterNames()) { + params.put(paramName, deNull(joinPoint.getArgs()[i++])); + } + return params; + } } diff --git a/src/main/java/it/gov/pagopa/apiconfig/selfcareintegration/util/Utility.java b/src/main/java/it/gov/pagopa/apiconfig/selfcareintegration/util/Utility.java index 8efa7bfc..03e17145 100644 --- a/src/main/java/it/gov/pagopa/apiconfig/selfcareintegration/util/Utility.java +++ b/src/main/java/it/gov/pagopa/apiconfig/selfcareintegration/util/Utility.java @@ -3,6 +3,8 @@ import it.gov.pagopa.apiconfig.selfcareintegration.model.PageInfo; import org.springframework.data.domain.Page; +import java.util.Optional; + public class Utility { private Utility() { @@ -17,4 +19,12 @@ public static PageInfo buildPageInfo(Page page) { .totalItems(page.getTotalElements()) .build(); } + + /** + * @param value value to deNullify. + * @return return empty string if value is null + */ + public static String deNull(Object value) { + return Optional.ofNullable(value).orElse("").toString(); + } }