controller日志切面

  • Post author:
  • Post category:其他




import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Aspect
@Component
@Slf4j
public class LogAspect {

  //com.kzj.kzj_rabbitmq.controller 包中所有的类的所有方法切面
  //@Pointcut("execution(public * com.kzj.kzj_rabbitmq.controller.*.*(..))")

  //只针对 MessageController 类切面
  //@Pointcut("execution(public * com.kzj.kzj_rabbitmq.controller.MessageController.*(..))")

  //统一切点,对com.kzj.kzj_rabbitmq.controller及其子包中所有的类的所有方法切面
  @Pointcut("execution(public * com.kzj.kzj_rabbitmq.controller..*.*(..))")
  public void Pointcut() {
  }

  //@Around:环绕通知
  @Around("Pointcut()")
  public Object Around(ProceedingJoinPoint pjp) throws Throwable {
    Map<String, Object> data = new LinkedHashMap<>();
    //获取request
    HttpServletRequest request = null;
    if ((RequestContextHolder.getRequestAttributes()) != null) {
      request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }
    //请求url
    if (request!=null && !Strings.isNullOrEmpty(request.getRequestURI())) {
      data.put("reqUrl", request.getRequestURI());
      List<String> s=new ArrayList<>();
      Enumeration<String> parameterNames = request.getParameterNames();
      while (parameterNames.hasMoreElements()) {
        String paramName = parameterNames.nextElement();
        s.add(paramName +"="+request.getParameter(paramName));
      }
      data.put("argument", Joiner.on(",").join(s));
    }
    //获取目标类名称
    String clazzName = pjp.getTarget().getClass().getName();
    //获取目标类方法名称
    String methodName = pjp.getSignature().getName();
    //记录请求参数
    Object[] args = pjp.getArgs();
    // try {
    //   List<String> list = new ArrayList<>();
    //   for (Object o : args) {
    //     list.add(new Gson().toJson(o));
    //   }
    //   data.put("params", list.toString());
    // } catch (Exception e) {
    //   log.error(new Gson().toJson(data), e.getMessage());
    // }
    //开始调用时间
    // 计时并调用目标函数
    long   start  = System.currentTimeMillis();
    Object result = pjp.proceed();
    //记录返回参数
    data.put("result", new Gson().toJson(result));
    //设置消耗总时间
    Long   time   = System.currentTimeMillis() - start;
    data.put("consumeTime", time + "ms");
    data.put("classMethod", clazzName + "#" + methodName);
    log.info("Aspect log:{}", data);
    return result;
  }

}



版权声明:本文为xl465564754原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。