10

I've got this simple bean for PerformanceMonitorInterceptor

@Configuration
@EnableAspectJAutoProxy
@Aspect
public class PerfMetricsConfiguration {
    /**
     * Monitoring pointcut.
     */
    @Pointcut("execution(* com.lapots.breed.judge.repository.*Repository.*(..))")
    public void monitor() {
    }

    /**
     * Creates instance of performance monitor interceptor.
     * @return performance monitor interceptor
     */
    @Bean
    public PerformanceMonitorInterceptor performanceMonitorInterceptor() {
        return new PerformanceMonitorInterceptor(true);
    }

    /**
     * Creates instance of performance monitor advisor.
     * @return performance monitor advisor
     */
    @Bean
    public Advisor performanceMonitorAdvisor() {
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression("com.lapots.breed.judge.repository.PerfMetricsConfiguration.monitor()");
        return new DefaultPointcutAdvisor(pointcut, performanceMonitorInterceptor());
    }
}

It supposed to trace any method invocation in the interfaces that ends with Repository in name. I set logging level in application.properties

logging.level.org.springframework.aop.interceptor.PerformanceMonitorInterceptor=TRACE

But during execution it doesn't write anything in the console. What's the problem?

kriegaex
  • 63,017
  • 15
  • 111
  • 202
lapots
  • 12,553
  • 32
  • 121
  • 242
  • Do you happen to be a colleague of the guy asking [this question](https://stackoverflow.com/q/49072611/1082681)?. Never having seen any question of that kind ever before and now two within a few days is unlikely to be pure chance. – kriegaex Mar 05 '18 at 11:30
  • @kriegaex no I don't know him, but I recall asking similar question some time ago as aspects rarely works from the very beginning in my code lol – lapots Mar 05 '18 at 11:34
  • Your point cut will obviously not mach. `setExpression` takes an expression not a reference to a `@Pointcut` annotated method. – M. Deinum Mar 05 '18 at 11:51
  • I took the code from here http://www.baeldung.com/spring-performance-logging – lapots Mar 05 '18 at 12:09
  • And did it ever work before you started changing the original? – kriegaex Mar 06 '18 at 13:22
  • @kriegaex I did not test and assumed it should work. I didn't really change anything except pointcut definition – lapots Mar 06 '18 at 13:23
  • @kriegaex I see. I thought there might be some evident issue – lapots Mar 06 '18 at 14:46
  • No, there is no evident issue. The original code from that other repo works for me. I quickly tried a while ago while waiting for my dinner delivery to arrive. – kriegaex Mar 06 '18 at 16:58
  • Question: Are your `*Repository` interfaces and/or the classes implementing them annotated with `@Component` and picked up as such by Spring? See, this is the problem when (a) you do not develop and debug step by step and (b) do not provide an [MCVE](http://stackoverflow.com/help/mcve). You know, the problem could just be in the code you do **not** show. – kriegaex Mar 06 '18 at 17:06

4 Answers4

15

I was facing similar issue, after changing the useDynamicLogger to false the issue was fixed.

@Bean
public PerformanceMonitorInterceptor performanceMonitorInterceptor() {
  return new PerformanceMonitorInterceptor(false);
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Manzoor
  • 151
  • 5
6

Faced with the same issue. And as Manzoor suggested passing false to PerformanceMonitorInterceptor solves the problem.

Why? When you call new PerformanceMonitorInterceptor(true), the logger name used inside of PerformanceMonitorInterceptor will be: com.lapots.breed.judge.repository.SomeClass.

So in your particular case the following logging configuration is required: logging.level.com.lapots.breed.judge.repository=TRACE, otherwise you do not see any logs, the breakpoint on PerformanceMonitorInterceptor.invokeUnderTrace() will not work and you spend lot's of time thinking you have wrong AOP configuration (while actually it's fine), but you did not set up logging level for proper class/package.

Kirill
  • 6,762
  • 4
  • 51
  • 81
4

I am using spring logging (SLF4J logging). Instead of putting PerformanceMonitorInterceptor logger to TRACE , I added com.lapots.breed.judge.repository logger to TRACE. This started printing logs for me.

I did this because the below method in AbstractTraceInterceptor is looking for TRACE enabled on the class(Repository) we executing but not on PerformanceMonitorInterceptor.

protected boolean isLogEnabled(Log logger) {
        return logger.isTraceEnabled();
    }
Ram
  • 363
  • 2
  • 10
1

I just tried this, I simply added this to application.properties and it works:

logging.level.org.springframework.aop.interceptor.PerformanceMonitorInterceptor=trace

Praveer
  • 101
  • 3
  • If this doesn't work, then probably you have some other logging config which is overriding this one. – Praveer Mar 21 '18 at 09:22