WebLogAspect.java 1.99 KB
package com.subsidy.common.configure;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@Aspect
@Component
@Order(1)
public class WebLogAspect {

    private final Logger logger = LoggerFactory.getLogger(WebLogAspect.class);

    @Pointcut("execution(public * com.subsidy.controller.*.*(..))")//切入点描述 这个是controller包的切入点
    public void controllerLog(){}//签名,可以理解成这个切入点的一个名称

    @Pointcut("execution(public * com.subsidy.controller.*.*(..))")
    public void serviceImplLog(){}

    @Before("controllerLog()") //在切入点的方法run之前要干的
    public void logBeforeController(JoinPoint joinPoint) {


        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        //这个RequestContextHolder是Springmvc提供来获得请求的东西
        HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();

        // 记录下请求内容
        logger.info("####requestUrl : " + request.getRequestURL().toString());
        logger.info("####requestParam : " + Arrays.toString(joinPoint.getArgs()));

    }

//    @Around("serviceImplLog()")
//    public Object afterController(ProceedingJoinPoint proceedingJoinPoint) {
//
//        Object result = null;
//
//        try{
//            result = proceedingJoinPoint.proceed();
//            logger.info("====reulst"+result);
//        }catch (Throwable e){
//            MyException myException =(MyException)e;
//            throw myException;
//        }
//        return result;
//    }

}