使用 Spring AOP 的 Web 应用程序中的日志记录不起作用

Logging not working in web application using Spring AOP

使用 Spring AOP,我正在尝试在我的 Web 应用程序中记录一个名为 corelation 的对象,如下所示:-

LoggingCorrelationEnrichingAspect.java:-

@Aspect
@Component
public class LoggingCorrelationEnrichingAspect {
    private static final Logger logger = getLogger(LoggingCorrelationEnrichingAspect.class);

    @Around("@annotation(Correlated)")
    public Object wrapWithCorrelationContext(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
            logger.info("Entering  "+ proceedingJoinPoint.getSignature().getName() +" with Correlation Id:: "
                    + ((Map)proceedingJoinPoint.getArgs()[0]).get(CommerceConnectorConstants.HttpHeaders.CORRELATION_ID).get());
        return ((Mono<?>) proceedingJoinPoint.proceed());
    }
}

Correlated.java:-

@Inherited
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Correlated {}

在我的主要 REST 控制器操作中,使用 @Correlated 注释,我尝试记录这种相关性,如下所示:-

  @Correlated
  @GetMapping(path = "/products}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public Mono<ProductBeanResponse> getProducts(
      @RequestHeader(name = Test.HttpHeaders.TENANT_ID, required = true) UUID tId,
      @RequestHeader(name = Test.HttpHeaders.CORRELATION_ID, required = true) UUID correlationId
----
---
}

但是,当我使用 PostMan 工具测试我的服务并查看应用程序日志时,从未记录相关 ID :-

logger.info("Entering  "+ proceedingJoinPoint.getSignature().getName() +" with Correlation Id:: "
                    + ((Map)proceedingJoinPoint.getArgs()[0]).get(CommerceConnectorConstants.HttpHeaders.CORRELATION_ID).get());

请告知这是 Spring AOP 中的配置问题。

谢谢

这可以通过以下两种方式之一工作

  1. 在切入点定义中提供 Correlated 的完全限定名称作为 @Around("@annotation(com.x.y.z.Correlated)")
  2. 更新方面方法签名以包含 Correlated 作为第二个参数

    @Around("@annotation(correlated)")
    public Object wrapWithCorrelationContext(ProceedingJoinPoint proceedingJoinPoint, Correlated correlated ) throws Throwable {
        logger.info("Entering  "+ proceedingJoinPoint.getSignature().getName() +" with Correlation Id:: "
                + ((Map)proceedingJoinPoint.getArgs()[0]).get(CommerceConnectorConstants.HttpHeaders.CORRELATION_ID).get());
    return ((Mono<?>) proceedingJoinPoint.proceed());
    }
    

如果还有其他需要,请在评论中告知。

P.S.:同样如 M. Deinum 所指出的,确保删除对象转换。