123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- package com.emato.ccnet.wx.aop;
- import com.fasterxml.jackson.core.JsonProcessingException;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import org.aspectj.lang.JoinPoint;
- import org.aspectj.lang.ProceedingJoinPoint;
- import org.aspectj.lang.Signature;
- import org.aspectj.lang.annotation.*;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.context.request.RequestContextHolder;
- import org.springframework.web.context.request.ServletRequestAttributes;
- import javax.servlet.http.HttpServletRequest;
- import java.util.Collection;
- import java.util.Map;
- /**
- * 日志AOP
- * 通过<code>JoinPoint</code>可以获得通知的签名信息,如目标方法名、目标方法参数信息等
- * 通过RequestContextHolder来获取<code>HttpServletRequest</code>请求信息,<code>Session</code>Session信息
- * @author Scott Chen
- * @since 1.0
- * 2015-05-13
- */
- @Aspect
- public class AspectWebLog {
- private static final Logger logger = LoggerFactory.getLogger(AspectWebLog.class);
- @Autowired
- ObjectMapper objectMapper;
- @Pointcut("execution(* com.emato.ccnet.wx..*.*(..)) " +
- "&& @annotation(org.springframework.web.bind.annotation.RequestMapping)" +
- "&& !execution(* com.emato.ccnet.wx..aop..*.*(..))")
- public void controllerPointcut(){}
- /**
- * 前置通知
- * 执行方法之前执行前置通知方法
- * @param jp:JoinPoint
- * @return
- */
- @Before(value="controllerPointcut()")
- public void beforeAdvice(JoinPoint jp) {
- // 目标方法的参数信息
- Object[] args = jp.getArgs();
- // AOP代理类的信息
- Object objThis = jp.getThis();
- // 代理的目标对象
- Object objTarget = jp.getTarget();
- // 通知的方法签名
- Signature signature = jp.getSignature();
- // AOP代理类类型
- Class proxyClzz = signature.getDeclaringType();
- // AOP代理类名
- String className = signature.getDeclaringTypeName();
- // 代理方法名
- String methodName = signature.getName();
- // 获取RequestAttributes
- ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
- // 从获取RequestAttributes中获取HttpServletRequest的信息
- HttpServletRequest request = attributes.getRequest();
- //method
- logger.debug("--- 请求Method: {}", request.getMethod());
- //类方法
- logger.debug("--- 调用class: 【{}】, method: 【{}】", className, methodName);
- try {
- String str = null;
- if (args.length > 0) {
- if (args[0] instanceof Collection || args[0] instanceof Map) {
- str = objectMapper.writeValueAsString(args[0]);
- } else {
- for(int i=0;i<args.length;i++) {
- str += objectMapper.writeValueAsString(args[i]);
- if (i < args.length - 1) {
- str += ", ";
- }
- }
- }
- }
- logger.debug("【" + className + "#" + methodName + "】发起请求, 请求参数: {}", str);
- } catch (JsonProcessingException e) {
- e.printStackTrace();
- }
- }
- /**
- * 环绕通知
- * 主体方法返回后将执行的通知方法
- * @param pjp:ProceedingJoinPoint
- * @return
- */
- @Around(value="controllerPointcut()")
- public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {
- String className = pjp.getSignature().getDeclaringTypeName();
- String methodName = pjp.getSignature().getName();
- long bftime = System.currentTimeMillis();
- // 继续执行被拦截的方法
- Object retVal = pjp.proceed();
- double time =(double)(System.currentTimeMillis() - bftime) / 1000;
- logger.info("【" + className + "#" + methodName + "】执行时间: {}秒", time);
- return retVal;
- }
- /**
- * 后置通知
- * 执行方法之后执行后置通知方法
- * @param jp:JoinPoint
- * @return
- */
- @After(value="controllerPointcut()")
- public void afterAdvice(JoinPoint jp) {
- Object[] args = jp.getArgs();
- String className = jp.getSignature().getDeclaringTypeName();
- String methodName = jp.getSignature().getName();
- try {
- String str = null;
- if (args.length > 0) {
- if (args[0] instanceof Collection || args[0] instanceof Map) {
- str = objectMapper.writeValueAsString(args[0]);
- } else {
- for(int i=0;i<args.length;i++) {
- str += objectMapper.writeValueAsString(args[i]);
- if (i < args.length - 1) {
- str += ", ";
- }
- }
- }
- }
- logger.info("【" + className + "#" + methodName + "】执行结束, 请求参数: {}", str);
- } catch (JsonProcessingException e) {
- e.printStackTrace();
- }
- }
- /**
- * 返回通知
- * 方法执行成功后,调用返回通知,如果方法在运行过程中抛出异常,则不会调用
- * @param jp:JoinPoint
- * @param retValue:String 主体方法传递到通知方法的返回值
- * @return
- */
- @AfterReturning(value="controllerPointcut()", returning = "retValue")
- public void afterReturningAdvice(JoinPoint jp, Object retValue) {
- String className = jp.getSignature().getDeclaringTypeName();
- String methodName = jp.getSignature().getName();
- if (retValue == null) {
- logger.info("【" + className + "#" + methodName + "】执行结束, 返回数据: {}");
- return;
- }
- try {
- String str = objectMapper.writeValueAsString(retValue);
- logger.info("【" + className + "#" + methodName + "】执行结束, 返回数据:{}", str);
- } catch (JsonProcessingException e) {
- e.printStackTrace();
- }
- }
- /**
- * 异常通知
- * 方法在运行过程中,如果抛出异常,则执行异常通知
- * @param jp:JoinPoint
- * @return
- */
- @AfterThrowing(value="controllerPointcut()", throwing = "ex")
- public void afterThrowingAdvice(JoinPoint jp, Exception ex) {
- String className = jp.getSignature().getDeclaringTypeName();
- String methodName = jp.getSignature().getName();
- logger.info("【" + className + "#" + methodName + "】发生异常结束: {}", ex);
- }
- }
|