3

I cannot figure out how to log the line number of the target method when logging entry/exit of methods using Spring AOP.

Things I have tried to get line number of the target method:

1) Using a Spring Aspect

This did not help because the SourceLocation always threw an UnsupportedOperationException whenever I tried to access the getLine() method.

    @Component
    @Aspect
    public class LoggingAspect {

      @Pointcut("execution(* com.xyz..*(..))")
      public void anyMethod() {}

      @Before("anyMethod()")
      public void entryLogging(JoinPoint joinPoint) throws Throwable {
        XLogger logger = XLoggerFactory.getXLogger(joinPoint.getSignature().getDeclaringType());

        Signature methodSignature = joinPoint.getSignature();

        logger.trace("Enter " + methodSignature.getDeclaringType().getCanonicalName() + ":" + methodSignature.getName() + ":" + joinPoint.getSourceLocation().getLine());

      }

     @After
     // excluded for brevity....
   }

2) Using a CustomizableTraceInterceptor

The problem with this is there does not seem to be a way of getting line numbers.

    @Configuration
    @EnableAspectJAutoProxy(proxyTargetClass = true)
    @Aspect
    public class TraceLoggerConfig {

      @Pointcut("execution(* com.xyz..*(..))")
      public void anyMethod() {}

      @Bean
      public CustomizableTraceInterceptor customizableTraceInterceptor() {
        CustomizableTraceInterceptor customizableTraceInterceptor = new CustomizableTraceInterceptor();
        customizableTraceInterceptor.setUseDynamicLogger(true);
        customizableTraceInterceptor.setEnterMessage("Entering $[targetClassName]:$[methodName]($[arguments])");
        customizableTraceInterceptor.setExitMessage("Leaving  $[methodName](), returned $[returnValue]");
        return customizableTraceInterceptor;
      }

      @Bean
      public Advisor loggingAdvisor() {
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression("com.xyz.TraceLoggerConfig.anyMethod()");
        return new DefaultPointcutAdvisor(pointcut, customizableTraceInterceptor());
      }

    }

Ideally the following things would happen:

1) I would not change my XML configured logging format (currently [%d{yyyy-MM-dd HH:mm:ss.SSS}] [%-5p] [%t] [%C:%M:%L] - %m%n). This is because I have other types of logging statements (besides trace) which benefit greatly from having this format.

2) My logging format would refer to the target method, rather than the AOP advice method. However Spring Aop logging line number incorrect makes me think this isn't possible through log4j configuration because it is how log4j works (uses the line number where the logging method was called).

I wouldn't mind specifying a custom format for logging within my Advice, where I would put the line number as part of the message log4j emits. But when I tried doing that my logger ignored my custom format, and just logged my first argument as my message. So even if I could get the line number of the target method invocation, the log format for these trace calls would be funky.

Community
  • 1
  • 1
Adam
  • 2,214
  • 1
  • 15
  • 26

0 Answers0