user3604456
user3604456

Reputation: 47

Aspect before call to org.apache.commons.logging.Log.warn

what I'd like to do, is have an aspect which listens to a call to org.apache.commons.logging.Log.warn and does something after the call.

Is that possible?

I created an aspect like this, but it doesn't ever get called when I do a LOG.warn:

@Pointcut("execution(* org.apache.commons.logging.Log.warn.*.*(..))")
public void warn() {}

@Around("warn()")
public void collect(JoinPoint joinPoint) throws Throwable {
  System.out.println("debug - 1"); // I have a breakpoint here that never gets hit
}

Test class:

private static final Log LOG = LogFactory.getLog(TestCollectMessagesAspect.class);

public String testCall(String param) {
    LOG.warn("this is a test");
    return param;
}

Thanks

Upvotes: 0

Views: 396

Answers (1)

kriegaex
kriegaex

Reputation: 67297

This is a typical pitfall for AspectJ beginners: You are trying to intercept method execution(), but in order to achieve this you need to weave your aspect into the target code, i.e. into Commons Logging in this case. In order to do that you would have to do binary weaving and create a woven version of the 3rd party JAR.

While this is possible, there is a simpler solution, provided you control the calling code and are only interested in warnings created by your own code, not by other 3rd party libs or so. If so, just switch from execution() to call() pointcuts. This will weave the callers rather than the callee.

Upvotes: 1

Related Questions