1

Tried below example but it's not working with spring.Getting err in editor like "call pointcut designator isn't supported by Spring".

https://dzone.com/articles/enforcing-common-log-format

Any code example would be appreciated.

  • Thank all for the help, I am anyway able to solve it by using Mapped Diagnostic Context. refer https://blog.trifork.com/2013/06/06/adding-user-info-to-log-entries-in-a-multi-user-app-using-mapped-diagnostic-context/ – Abinash Pattnaik Nov 29 '18 at 06:25

3 Answers3

0

There are many ways to do this, here is 1.

1) Create an annotation to add to classes/methods that need logging added:

@Documented
@Target(ElementType.METHOD)
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface LogExecution {    
}

2) Create an aspect to do the logging:

@Aspect
@Component
public class LogAspect {

    private List<String> messages = new ArrayList<>();

    @Around("@annotation(hello.LogExecution)")
    public Object handelLogging(ProceedingJoinPoint joinPoint) throws Throwable {

        Object proceed = null;
        try {
           // log before
           proceed = joinPoint.proceed();
           // log after
        }
        catch (Exception e) {
           // log exception
           throw e;
        }
        return proceed;
    }    
}

Working example here

Essex Boy
  • 7,565
  • 2
  • 21
  • 24
  • Thanks for the reply. Actually this is not my requirement. I want to target all log messages(slf4j logger ) that present inside doSomething() method(as per your example), and prefix some text dynamically through the aspect class. – Abinash Pattnaik Nov 27 '18 at 06:45
0

It sounds like you don't have Aspect J correctly configured.

See the answer to this question for the correct setup:

Spring + AspectJ weaving for java 8 using aspectj-maven-plugin

After that, spring should recognize your pointcuts

mad_fox
  • 3,030
  • 5
  • 31
  • 43
  • This works for your own code but not for Spring or other 3rd party JARs, so this is not an option. The OP wants to apply his aspect code to Spring core code, if I understand correctly. – kriegaex Dec 11 '18 at 08:04
0

The canonical way to use AspectJ from within Spring applications as described by the manual is to use AspectJ load-time weaving (LTW). It does not make sense to weave code into Spring or Java EE binaries and create new JARs etc., so IMO source or binary compile-time weaving is not an option if you want to target 3rd party code. Instead, a dynamic Java weaving agent as described in the manual chapter I linked to above.

Having said that, it was just a reply to the AOP-related suggestions I read about here. It would work the way I described it, but I am happy the OP has solved his problems by means of Mapped Diagnostic Context already.

kriegaex
  • 63,017
  • 15
  • 111
  • 202