Spring pointj pointcut when registering with slf4j

I am working with spring 3.0.6. In my application there are many places where registration is added (slf4j). Let's say I need to add some function for every serious error - I better catch every call to register the error level and then follow it - send mail for support with an exception message or smth like this - than manually add the code to all places in the application .

I created the 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 works well with my classes, but nothing happened for org.slf4j.Logger

+4
source share
2 answers

@ crudo6, this will not work with Spring @AspectJ support using proxies - the reason is that the Spring method handles the @AspectJ annotation to create proxies, for example. if you @Around advise for your @PointCut("execution (for your class)") , then Spring will create a proxy for all beans in the Spring Context with types that correspond to the class in pointcut.

Now, since the 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 make them work, you can either try to bind time in time, or compile time and use "@Pointcut" ("call (* org.slf4j.Logger.error (..))") instead of making it, so that any calls SLF4J may be intercepted by your advice. @Pointcut execution will require weaving slf4j libraries, which may not be possible.

+6
source

Sending mail synchronously subject to an error is bad practice. You can send too many emails that can hold threads for a longer time and affect application performance and scalability. The best approach is to use a log monitoring tool such as Splunk.

0
source

Source: https://habr.com/ru/post/1402392/


All Articles