2
votes

I am trying to apply around advice over logger calls to modify the log message. As Logger frameworks are used in most libraries, I would like to capture only logger calls from a specific package.

    @Aspect
    public class LogAspect {

    @Pointcut("within(com.testing.servlet..*) && execution(* org.slf4j.Logger.debug(..))")
        public void logging() {

        }

    @Around("com.testing.aspect.LogAspect.logging()")
        public void around(ProceedingJoinPoint jp) {
            Object[] args = jp.getArgs();
            String modifiedLogMessage = "Appended Log - ";
            for (int i = 0; i < args.length; i++) {
                if (args[i] instanceof String) {
                    modifiedLogMessage += (String) args[i];
                    args[i] = modifiedLogMessage;
                }
            }
            try {
                jp.proceed(args);
            } catch (Throwable e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
}

I have tried different possibilities and it doesn't work,
@Pointcut("within(com.testing.logging.)) - captures all debug calls under specific package
@Pointcut("execution(
org.slf4j.Logger.debug(..))") - captures all debug calls under every package
@Pointcut("within() && execution( org.slf4j.Logger.debug(..))") - captures all debug calls under every package
@Pointcut("within(com..) && execution( org.slf4j.Logger.debug(..))") - doesn't capture anything

I am missing something basically and confused on how to apply the pointcut properly

aop.xml

    <!DOCTYPE aspectj PUBLIC
        "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
    <weaver options="-verbose -showWeaveInfo">
        <!-- <include within="com.testing.*" />
        <include within="org.slf4j.*"/> -->
    </weaver>
    <aspects>
        <aspect name="com.testing.aspect.LogAspect" />
        <!-- <concrete-aspect name="com.testing.aspect._My_AbstractAspect"
            extends="com.testing.aspect.AbstractLogAspect">
            <pointcut name="scope" expression="execution(* org.slf4j.Logger.debug(..))" />
        </concrete-aspect> -->
        <!-- <include within="com.testing.*" /> -->
    </aspects>
</aspectj>

I have tried multiple combination in aop.xml too, but none seems to be working. Thanks for your help in advance

1
Have you tried using in your pointcut "call" instead of "execution"? cannot tell you exactly why, but it worked for me... - sas

1 Answers

0
votes

I had a similar problem and I think this may work for you:

within (com.your.package) && call (* org.slf4j.Logger.debug(..))

Some slightly different call argument syntax may be needed. This seemed to work for me. My specific case was around catching calls to Thread.sleep(), but I only wanted to catch calls from my package, so I used the following:

@Around("within (com.mycompany.mypackage) && call (void java.lang.Thread.sleep(long))")

I think the key difference is that "execution" is catching where calls to a method happen on the callee side, but "call" is catching calls on the caller side. So, essentially, the "execution" is happening inside the "org.slf4j.Logger" package hence when you further filter by "and within my package" there is no overlap. But, the "calls" happen from your package (and other packages) so that does make sense to intersect with your package.