实现Web层的日志切面(方便清晰查看日志)

 
  1 import org.aspectj.lang.JoinPoint;
  2 import org.aspectj.lang.Signature;
  3 import org.aspectj.lang.annotation.AfterReturning;
  4 import org.aspectj.lang.annotation.Aspect;
  5 import org.aspectj.lang.annotation.Before;
  6 import org.aspectj.lang.annotation.Pointcut;
  7 import org.aspectj.lang.reflect.MethodSignature;
  8 import org.slf4j.Logger;
  9 import org.slf4j.LoggerFactory;
 10 import org.springframework.core.annotation.Order;
 11 import org.springframework.stereotype.Component;
 12 import org.springframework.web.context.request.RequestContextHolder;
 13 import org.springframework.web.context.request.ServletRequestAttributes;
 14 
 15 import javax.servlet.http.HttpServletRequest;
 16 import java.io.UnsupportedEncodingException;
 17 import java.net.URLDecoder;
 18 
 19 /**
 20  * 实现Web层的日志切面
 21  * @author lpf
 22  */
 23 @Component
 24 @Aspect
 25 @Order(1)
 26 public class WebLogAspect {
 27     private Logger log = LoggerFactory.getLogger(getClass());
 28     private ThreadLocal<Long> startTime = new ThreadLocal<>();
 29 
 30     /**
 31      * 定义一个切入点.
 32      * 解释下:
 33      * <p>
 34      * ~ 第一个 * 代表任意修饰符及任意返回值.
 35      * ~ 第二个 * 任意包名
 36      * ~ 第三个 * 定义在web包或者子包
 37      * ~ 第四个 * 任意方法
 38      * ~ .. 匹配任意数量的参数.
 39      */
 40     // @Pointcut("execution(public * com.kfit.*.web..*.*(..))")
 41     // use
 42     // @Pointcut("execution(public * org.zhilan..*Controller.*(..))")
 43 
 44     // @Pointcut("(execution(public * org.zhilan.timer.*Service.*(..)))")  
 45     @Pointcut("(execution(public * com.haoyun..*Controller.*(..))) "
 46             // + "|| execution(public * com.haoyun.timer.*Service.*(..)) "
 47             + "&& !execution(* com.haoyun.login.LoginController.getSystemState())"
 48     )
 49     public void webLog() {
 50     }
 51 
 52     @Before("webLog()")
 53     public void doBefore(JoinPoint joinPoint) throws UnsupportedEncodingException {
 54         startTime.set(System.currentTimeMillis());
 55 
 56         // 接收到请求,记录请求内容
 57         log.info("========================= before start =========================");
 58         Signature signature = joinPoint.getSignature();
 59         MethodSignature methodSignature = (MethodSignature) signature;
 60 
 61         log.info("CLASS_METHOD : " + methodSignature.getDeclaringTypeName() + "." + methodSignature.getName());
 62 
 63         // 记录下请求内容
 64         ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
 65         if(attributes != null){
 66             HttpServletRequest request = attributes.getRequest();
 67             log.info("IP : " + request.getRemoteAddr());
 68         }
 69 
 70         log.info("请求参数:    名称     值");
 71         String[] argsNameArray  = methodSignature.getParameterNames();
 72         Object[] argsValueArray = joinPoint.getArgs();
 73         for (int i = 0; i < argsNameArray.length; i++) {
 74             log.info("args_name: " + argsNameArray[i]);
 75 
 76             String argValue = argsValueArray[i] != null ? argsValueArray[i].toString() : "";
 77             if (argsNameArray[i].contains("encode")) {
 78                 String str = URLDecoder.decode(argValue, "utf-8");
 79                 log.info("args_value: " + (str.length() > 200 ? str.substring(0, 200) + "..." : str));
 80             } else {
 81                 log.info("args_value: " + (argValue.length() > 200 ? argValue.substring(0, 200) + "..." : argValue));
 82             }
 83         }
 84 
 85         log.info("========================= before end =========================");
 86     }
 87 
 88     @AfterReturning(returning="rvt", pointcut="webLog()")
 89     public void doAfterReturning(JoinPoint joinPoint, Object rvt) {
 90         // 处理完请求, 返回内容
 91         log.info("========================= after returning start =========================");
 92         Signature signature = joinPoint.getSignature();
 93         log.info("CLASS_METHOD : " + signature.getDeclaringTypeName() + "." + signature.getName());
 94 
 95         if(rvt != null){
 96             String str = rvt.toString();
 97             if (str.length() > 200) {
 98                 str = str.substring(0, 200) + "...";
 99             }
100 
101             log.info("return 返回值:");
102             log.info(str);
103         }
104 
105         log.info("耗时(毫秒) : " + (System.currentTimeMillis() - startTime.get()));
106 
107         log.info("========================= after returning end =========================");
108     }
109 }

 

posted @ 2018-02-12 20:23  Other+  阅读(267)  评论(0编辑  收藏  举报