aatuc210
aatuc210

Reputation: 1868

How to prevent my Spring logging aspect from firing?

My Spring Boot code has a logging aspect that records the time it takes for a method to complete. There are some classes that I don't want this behavior for, so I created a NoLogging annotation to place on the class I don't want to run the aspect for. But it's not working at all. I'm still seeing the aspect run on the classes with my annotation. I don't understand Spring AOP enough to know exactly where I define the !@annotation(org.apa.idem.sso.aspect.logging.NoLogging). Can someone help?

Here is the implementation of the aspect:

@Component
@Aspect
public class IdemSsoLoggingAspect {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 
     * This method will calculate the Time taken by the public methods
     * 
     * 
     * @param joinPoint
     * @return
     * @throws Throwable
     */

    private final static String executionString = "!@annotation(org.apa.idem.sso.aspect.logging.NoLogging) && execution(public * org.apa.idem.sso.rest.*.*(..)) || execution(public * org.apa.idem.sso.web.controller.*.*(..)) || execution(public * org.apa.idem.sso.service.*.*(..)) ||   @annotation(org.apa.idem.erights.Transactional)";

    @Around(executionString)
    public Object logTimeMethod(ProceedingJoinPoint joinPoint) throws Throwable {
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            Object retVal = joinPoint.proceed();
            stopWatch.stop();
            StringBuffer logMessage = new StringBuffer();
            logMessage.append(" execution time: ");
            logMessage.append(stopWatch.getTotalTimeMillis());
            logMessage.append(" ms");
            logMessage.append(joinPoint.getTarget().getClass().getName());
            logMessage.append(".");
            logMessage.append(joinPoint.getSignature().getName());
            logMessage.append("(");
            // append args
            Object[] args = joinPoint.getArgs();
            for (int i = 0; i < args.length; i++) {
                logMessage.append(args[i]).append(",");
            }
            if (args.length > 0) {
                logMessage.deleteCharAt(logMessage.length() - 1);
            }
            logMessage.append(")");
            logger.error("****************** Time taken  " + logMessage.toString());
            return retVal;
    }
}

Can someone tell me what I'm doing wrong?

Upvotes: 0

Views: 1022

Answers (1)

Simon Martinelli
Simon Martinelli

Reputation: 36123

You have to put the second part of the condition in parenthesis because you have OR operators that will be true.

So the first part is the "not annotation" and the second part is everything else but in parenthesis.

!@annotation(org.apa.idem.sso.aspect.logging.NoLogging) 
    && (execution(public * org.apa.idem.sso.rest.*.*(..)) 
       || execution(public * org.apa.idem.sso.web.controller.*.*(..)) 
       || execution(public * org.apa.idem.sso.service.*.*(..)) 
       || @annotation(org.apa.idem.erights.Transactional))

And also in the pointcut:

 @Pointcut("(within(@org.springframework.stereotype.Repository *)" +
    " || within(@org.springframework.stereotype.Service *)" +
    " || within(@org.springframework.web.bind.annotation.RestController *))"
    + " && !@annotation(org.apa.idem.sso.aspect.logging.NoLogging)")

Upvotes: 1

Related Questions