Blame view

WebLogAspect.java 1.93 KB
涂亚平 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
package com.meishu.common.configure;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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
public class WebLogAspect {

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

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

    @Pointcut("execution(public * com.meishu.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;
//    }

}