簡體   English   中英

使用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 Controller操作中,使用@Correlated批注,我正在嘗試記錄此corellation,如下所示:-

  @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工具測試服務並查看應用程序日志時,從未記錄corelation id:-

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

請告知這是Spring AOP中的配置問題。

謝謝

這可以通過以下兩種方式之一進行

  1. 在切入點定義中以@Around("@annotation(com.xyzCorrelated)")提供Correlated完全限定名稱。
  2. 更新Aspect方法簽名以將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()); } 

如果需要其他說明,請在評論中告知。

PS :也正如M. Deinum所指出的那樣,請確保除去物體。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM