0
votes

I am using Spring AOP for logging purpose. Everything is working but in the log i am only getting the name of the Aspect class. I want the respective name of the class to be printed. Here is the code snippet. What changes are required in the following code to suffice my requirement.

private static final Logger logger = Logger.getLogger(LogginAspect.class);

@After("execution(* com.app.c2pc...(..))") public void logAfter(JoinPoint joinPoint) {

    logger.info("After executing method : " + joinPoint.getSignature().getName());
    logger.info("***************************************************************************");
}

@Before("execution(* com.app.c2pc..*.*(..))")
public void logBefore(JoinPoint joinPoint) {

    logger.info("***************************************************************************");
    logger.info("Before executing method : " + joinPoint.getSignature().getName());
}

@Around("execution(* com.app.c2pc..*.*(..)) && !execution(* com.app.c2pc.login.LoginController.*(..)) ")
public Object logAround(ProceedingJoinPoint pjp) throws Throwable {

    long start = System.currentTimeMillis();
    Object clazz = pjp.getTarget().getClass().getName();
    String methodName = pjp.getSignature().getName();
    logger.info("Entering Class " + clazz + " With Method Name " + methodName);
    Object[] obj = pjp.getArgs();
    int i = 0;
    try {
        for (Object o : obj) {
            logger.info(++i + " : Parameter Name :" + (null != o ? o.toString() : ""));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    Object output = pjp.proceed(pjp.getArgs());

    logger.info("Excecution Completed for method : " + methodName + " in Class : " + clazz + " with result "
            + output);
    long elapsedTime = System.currentTimeMillis() - start;
    logger.info("Execution time for method : " + methodName + " in Class : " + clazz + " : " + elapsedTime
            + " milliseconds.");
    return output;
}

@AfterThrowing(pointcut = "execution(* com.app.c2pc..*.*(..))", throwing = "error")
public void logAfterThrowing(JoinPoint joinPoint, Throwable error) {

    logger.info("Exception thrown by method : " + joinPoint.getSignature().getName());
    logger.info("Exception name : " + error);
}

@AfterReturning(pointcut = "execution(* com.app.c2pc..*.*(..))", returning = "result")
public void logAfterReturning(JoinPoint joinPoint, Object result) {

    logger.info("Method : " + joinPoint.getSignature().getName() + " returned value is : " + result);
}

This is the o/p i get in the log file:-

2016-07-20 23:41:20 | | INFO LogginAspect:23 - After executing method : checkSubmitEvalFlag 2016-07-20 23:41:20 | | INFO LogginAspect:24 - *************************************************************************** 2016-07-20 23:41:20 | | INFO LogginAspect:70 - Method : checkSubmitEvalFlag returned value is : 0 2016-07-20 23:41:20 | | INFO LogginAspect:40 - Entering Class com.app.c2pc.scie.daoImpl.PbdDAOImpl$$EnhancerBySpringCGLIB$$688f6ece With Method Name getBackUpUserDetails 2016-07-20 23:41:20 | | INFO LogginAspect:45 - 1 : Parameter Name :com.app.c2pc.scie.bean.SearchCriteriaBean@77ef5b02

1

1 Answers

-1
votes

you can use "joinPoint.getTarget().getClass().getName()" to get fully qualified class name like "com.abc.pqr.MyClass"

you can use "joinPoint.getTarget().getClass().getSimpleName()" to get only class name like "MyClass".