Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log actual repository class names using spring AOP instead of proxy?

i am trying to log repository class name using AOP. But at run time i am getting the proxy class name in logs as i have used spring data in my project so there is an interface used for repository which gets implemented on the fly by spring. please suggest an answer as i have spent lot of time finding same.

Following is the log generated after execution.

    className - No of records returned : 38
  2017-02-03 19:54:43,360 [INFO]   className - Exiting  method:  getAlertPatterns]
  2017-02-03 19:54:43,360 [INFO]   className - No of records returned : 38
  2017-02-03 19:54:43,360 [INFO]   className - Exiting  method:  getAlertPatterns]
  2017-02-03 19:54:43,363 [INFO]   controllerClassName - Exiting method:  getAlertPatterns]
  2017-02-03 19:54:46,457 [INFO]   controllerClassName - ActionTakenController***Entering  method:  getAlertActions]
  2017-02-03 19:54:46,458 [INFO]   className - ActionTakenServiceImpl****Entering  method:  getAlertActions]
  2017-02-03 19:54:46,458 [INFO]   className - $Proxy158****Entering  method:  getAlertActions]
  2017-02-03 19:54:46,510 [INFO]   repositoryClassName - java.util.ArrayList***
  2017-02-03 19:54:46,511 [INFO]   repositoryClassName - No of records returned : 2
  2017-02-03 19:54:46,511 [INFO]   repositoryClassName - Exiting method:  getAlertActions]
  2017-02-03 19:54:46,511 [INFO]   className - No of records returned : 2
  2017-02-03 19:54:46,511 [INFO]   className - Exiting  method:  getAlertActions]
  2017-02-03 19:54:46,511 [INFO]   className - No of records returned : 2
  2017-02-03 19:54:46,512 [INFO]   className - Exiting  method:  getAlertActions]
  2017-02-03 19:54:46,512 [INFO]   controllerClassName - Exiting method:  getAlertActions]
  2017-02-03 19:54:50,488 [INFO]   controllerClassName - InitialAnalysisController***Entering  method:  getAlertInitialAnalysis]
  2017-02-03 19:54:50,495 [INFO]   className - InitialAnalysisServiceImpl****Entering  method:  getAlertInitialAnalysis]
  2017-02-03 19:54:50,505 [INFO]   className - $Proxy144****Entering  method:  getAlertInitialAnalysis]

code used for configuring aop class is as follow

    private Logger logger = LogManager.getLogger("");

    /** Pointcut for execution of methods on {@link Service} annotation */
    @Pointcut("execution(public * (@org.springframework.web.bind.annotation.RestController com.nscorp.apps.wds.controller..*).*(..))")
    public void controllerCalls() {
        /** Pointcut for execution of methods on {@link Service} annotation */
    }

    /** Pointcut for execution of methods on {@link Service} annotation */
    @Pointcut("execution(public * (@org.springframework.stereotype.Service com.nscorp.apps.wds.services..*).*(..))")
    public void serviceAnnotation() {
        /** Pointcut for execution of methods on {@link Service} annotation */
    }

    /** Pointcut for execution of methods on {@link Repository} annotation */
    @Pointcut("execution(public * (@org.springframework.stereotype.Repository com.nscorp.apps.wds.dao..*).*(..))")
    public void repositoryAnnotation() {
        /** Pointcut for execution of methods on {@link Repository} annotation */
    }

    /** Pointcut for execution of methods on {@link JpaRepository} interfaces */
    @Pointcut("this (org.springframework.data.repository.Repository)")
    public void jpaRepository() {
        /** Pointcut for execution of methods on {@link JpaRepository} interfaces */
    }
    /** Pointcut for execution of combined  methods serviceAnnotation() and controllerCalls() */
    @Pointcut("serviceAnnotation()   || jpaRepository()  ")
    public void performanceMonitor() {
        /** Pointcut for execution of combined  methods serviceAnnotation() and controllerCalls() */
    }

    /** Pointcut for execution of combined  methods repositoryAnnotation() and jpaRepository()*/
    @Pointcut(" jpaRepository()  ")
    public void jpaTransactionlogs() {
        /** Pointcut for execution of combined  methods repositoryAnnotation() and jpaRepository()*/
    }

    @Pointcut(" controllerCalls() ")
    public void controllerParamCalls() {
        //buisenessCalls
    }


    @Around("controllerParamCalls()")
    public Object controllerClassName(ProceedingJoinPoint joinPoint) throws Throwable {
        logger.info(joinPoint.getTarget().getClass().getSimpleName()+"***" +"Entering  method:  " + joinPoint.getSignature().getName() + "]");

           Object[] signatureArgs = joinPoint.getArgs();
           for (Object signatureArg: signatureArgs) {
               logger.info( "The arguments are ----" + signatureArg);
           }


        Object obj = joinPoint.proceed();
        if (obj instanceof Collection) {
            logger.info("No of records returned : " + ((Collection<?>) obj).size());

        }
        logger.info("Exiting method:  " + joinPoint.getSignature().getName() + "]");
        return obj;
    }

    @Around("performanceMonitor()")
    public Object className(ProceedingJoinPoint joinPoint) throws Throwable {
        logger.info(joinPoint.getTarget().getClass().getSimpleName() +"****" +"Entering  method:  " + joinPoint.getSignature().getName() + "]");
        Object obj = joinPoint.proceed();
        if (obj instanceof Collection) {
            logger.info("No of records returned : " + ((Collection) obj).size());

        }
        logger.info("Exiting  method:  " + joinPoint.getSignature().getName() + "]");
        return obj;
    }

sample repository class is

    @Repository
public interface ActionTakenRepository extends CrudRepository<ActionTaken, BigInteger> {

    @Query("Select actionTaken from ActionTaken actionTaken where actionTaken.status NOT IN (Select codeId from Codes where codeType='"+CommonConstants.INACTIVE+"') order by auditCreatedAt desc")
    List<ActionTaken> getAlertActions(); 
}
like image 322
saurabhmalpani Avatar asked Feb 03 '17 14:02

saurabhmalpani


2 Answers

If I understand the questions correctly you are wanting to pull the interfaces which would be in this array

joinPoint.getThis().getClass().getGenericInterfaces();

You can comb through that for the name of the interface.

like image 84
CCrawford Avatar answered Sep 28 '22 06:09

CCrawford


instead of joinPoint.getTarget().getClass().getSimpleName() try using

joinPoint.getSignature().getDeclaringTypeName()

to get the actual class name (including package).

Also, to get the executed method name:

joinPoint.getSignature().getName()

joinPoint is of type org.aspectj.lang.ProceedingJoinPoint

like image 32
Denis.Kipchakbaev Avatar answered Sep 28 '22 06:09

Denis.Kipchakbaev