I'm working with spring 3.0.6. My app has lot of places where logging added (slf4j). Say I need to add some function on every serious error - it will be better for me to catch every call to error level logging and do may job after it - send mail to support with exception message, or smth like that - than manually add code to all that places in app.
I ve created following class:
@Aspect
public class LoggingWrapper {
@Pointcut("execution (* org.slf4j.Logger.error(..))")
public void logError() {
}
@AfterReturning("logError()")
public void afterError() {
//System.out.println("LOGERROR ASPECT AFTER");
//send email...
}
}
In spring config:
<aop:aspectj-autoproxy />
<bean id="loggingWrapper" class="com.app.services.LoggingWrapper"/>
Aspect is working well with my classes, but for org.slf4j.Logger - nothing happened
@crudo6, this will not work with Spring @AspectJ support using proxies - the reason is the way Spring handles @AspectJ annotation is to create proxies, for eg. if you @Around
advice for your @PointCut("execution (for your class)")
, then Spring would create proxies for all beans in the Spring Context with types which match the class in pointcut.
Now since slf4j classes are not part of the Spring context, a proxy will not be created for them and your aspects will not take effect.
To get them to work, you can either try load time weaving or compile time weaving and use '@Pointcut("call (* org.slf4j.Logger.error(..))")' instead of execution, this way any calls to SLF4J can be intercepted by your advice. @Pointcut of execution will need weaving the slf4j libraries which may not be possible.