How to make logging facility in project using AOP in spring

Logging Facility using AOP Spring


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

public class LoggingAspect
    private Log log = LogFactory.getLog(this.getClass());
    //@Pointcut("execution(* *.*(..))")
    protected void loggingOperation() {}
    public void logJoinPoint(JoinPoint joinPoint)
    {"Join point kind : " + joinPoint.getKind());"Signature declaring type : "+ joinPoint.getSignature().getDeclaringTypeName());"Signature name : " + joinPoint.getSignature().getName());"Arguments : " + Arrays.toString(joinPoint.getArgs()));"Target class : "+ joinPoint.getTarget().getClass().getName());"This class : " + joinPoint.getThis().getClass().getName());
     StringBuilder sb = new StringBuilder(joinPoint.getSignature().getDeclaringTypeName());
     sb.append(" -->");
     sb.append(joinPoint.getSignature().getName()+"() Method Start");;
    @AfterReturning(pointcut="loggingOperation()", returning = "result")
    public void logAfter(JoinPoint joinPoint, Object result)
    {"Exiting from Method :"+joinPoint.getSignature().getName());"Return value :"+result);

     StringBuilder sbLogAfter = new StringBuilder(joinPoint.getSignature().getDeclaringTypeName());
     sbLogAfter.append(" -->");
     sbLogAfter.append(joinPoint.getSignature().getName()+"() Method End");;
    @AfterThrowing(pointcut="execution(**.*(..))", throwing = "e")
   // @AfterThrowing(pointcut="execution(* *.*(..))", throwing = "e")
    public void logAfterThrowing(JoinPoint joinPoint, Throwable e)
     log.error("An exception has been thrown in "+ joinPoint.getSignature().getName() + "()");
        log.error("Cause :"+e.getCause());
        StringBuilder sbLogAfter = new StringBuilder("An exception has been thrown in "+joinPoint.getSignature().getDeclaringTypeName());
     sbLogAfter.append(" -->");
     sbLogAfter.append(joinPoint.getSignature().getName()+"() Method End");
//    @Around("execution(* *.*(..))")
    public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable
    {"The method " + joinPoint.getSignature().getName()+ "() begins with " + Arrays.toString(joinPoint.getArgs()));
            Object result = joinPoint.proceed();
  "The method " + joinPoint.getSignature().getName()+ "() ends with " + result);
            return result;
        } catch (Throwable e)
            log.error("Illegal argument "+ Arrays.toString(joinPoint.getArgs()) + " in "+ joinPoint.getSignature().getName() + "()");
            throw e;

Note : Above code is usefull for spring annotation based project, We are required only one class to activate logging facility in our system. No need to define logger for all method in our code , when we entered into method and goes out from the method.

Following changes would required to implement logging facility in spring project.

File Name : spring-servlet.xml

Following two library must be avaiable in your class path with it's dependant library.
  1. spring-aop-3.2.3.RELEASE.jar
  2. spring-aspects-3.2.3.RELEASE.jar
How it's work ? 



Post a Comment

Please provide your valuable comment.

Newer Post Older Post Home


Powered by Blogger.


Custom Search