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