javaloggingaspectjload-time-weaving

Use Aspectj load time weaving for logging my source (include test) without logging external libraries


I am using AspectJ for logging when tests execution only, so I use load time weaving. I pack Interceptor to a jar file to use with another Maven project. But with below config, aspectjweaver will weave methods of external libraries. I want it only weave my source code (include test) without specific config like <include within="hello.*"/>, for generic using like dependency.

Sorry, my English is quite bad. Thanks so much !!!

In aop.xml of this jar file, it likes

<aspectj>
<aspects>
    <aspect name="log.Interceptor"/>
    <weaver options="-verbose -showWeaveInfo">
        <include within="*"/>
    </weaver>
</aspects>
</aspectj>

// Interceptor

pointcut traceMethods() : (execution(* *(..)) && !cflow(within(Interceptor)) && !within(*Test) && !within(Test*) && !within(*Tests) && !within(*TestCase));
before(): traceMethods(){
    Method method = ((MethodSignature) thisJoinPointStaticPart.getSignature()).getMethod();
    logDebug(method, LogPattern.METHOD_START);
}
after(): traceMethods(){
    Method method = ((MethodSignature) thisJoinPointStaticPart.getSignature()).getMethod();
    logDebug(method, LogPattern.METHOD_FINISH);
}` 

Solution

  • Well, you cannot eat the cake and keep it at the same time. So either your solution is generic with pointcuts targeting the whole world or it is specific via includes and/or excludes.

    Both suggested solutions should work, but they require your library's users to have a basic understanding of AspectJ or at least pointcut syntax. Either you do that and document your library accordingly with sample pointcuts or whole sample AOP config files or you have to be more specific in your own pointcuts and/or in-/excludes. There is no magic solution, the AspectJ weaver cannot guess what you want to weave and what not.