Spring AOP logging request information

Keywords: Spring

Spring has two major cores, IOC and AOP. You know the theory of AOP, but have you ever used AOP? How to use it? This requires practical experience.

Here is a case of using AOP to record request logs:

case

  1. Define the pointcut, which is the annotation using @ RequestMapping
@Pointcut(value = "@annotation(org.springframework.web.bind.annotation.RequestMapping)")
  1. Using surround enhancement, get the method name of the call and the parameters passed
 @Around("pointcut()")
    public Object log (ProceedingJoinPoint joinPoint) throws Throwable {
        Object proceed = joinPoint.proceed();
        Object[] args = joinPoint.getArgs();
        if(null==args || 0==args.length){
            return proceed;
        }
        //Get full method name
        String methodName = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();
        String pramer=null;
        try {
            Map<Integer, Object> map = new HashMap<Integer, Object>();
            for (int i = 0; i < args.length; i++) {
                Object obj = diffLog(args[i]);
                if (null != obj) {
                    if (obj instanceof String) {
                        pramer=(String)obj;
                        continue;
                    }
                    map.put(i, obj);
                }
            }
            pramer += JSONObject.toJSONString(map);
            logger.error("The calling method name is:["+methodName+"]\n   Admission is:{} \n The reference is:{}",pramer,JSONObject.toJSONString(proceed));
        }catch (Exception e){
            logger.error("Exception in logging,The exception log is{}",e);
        }
        return proceed;
    }

  1. Just configure the facet as a bean and enable Spring AOP. Reference here Getting started with Spring AOP
<aop:aspectj-autoproxy proxy-target-class="true"/>

So when we request our project, we can record the request information

Full code:

@Component
@Aspect
public class LogAspect {

    private static final Logger logger= LoggerFactory.getLogger(getClass());

    @Pointcut(value = "@annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public void pointcut(){
    }
    @Around("pointcut()")
    public Object log (ProceedingJoinPoint joinPoint) throws Throwable {
        Object proceed = joinPoint.proceed();
        Object[] args = joinPoint.getArgs();
        if(null==args || 0==args.length){
            return proceed;
        }
        //Get full method name
        String methodName = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();
        String pramer=null;
        try {
            Map<Integer, Object> map = new HashMap<Integer, Object>();
            for (int i = 0; i < args.length; i++) {
                Object obj = diffLog(args[i]);
                if (null != obj) {
                    if (obj instanceof String) {
                        pramer=(String)obj;
                        continue;
                    }
                    map.put(i, obj);
                }
            }
            pramer += JSONObject.toJSONString(map);
            logger.error("The calling method name is:["+methodName+"]\n   Admission is:{} \n The reference is:{}",pramer,JSONObject.toJSONString(proceed));
        }catch (Exception e){
            logger.error("Exception in logging,The exception log is{}",e);
        }
        return proceed;
    }

    /**
     * Classification of logs
     * @param parm
     */
    private Object diffLog(Object parm) {
        ...
    }
}


Last

Here is an example of using AOP to record logs. In addition to logging, it can also be used for monitoring. There are many uses, which need to be explored

Reference resources

Posted by PHP Novice on Wed, 04 Dec 2019 23:54:22 -0800