|
|
@@ -0,0 +1,107 @@
|
|
|
+package com.webflux.launchadmin.aspect;
|
|
|
+
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import jakarta.servlet.http.HttpServletRequest;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.aspectj.lang.JoinPoint;
|
|
|
+import org.aspectj.lang.annotation.AfterReturning;
|
|
|
+import org.aspectj.lang.annotation.Aspect;
|
|
|
+import org.aspectj.lang.annotation.Before;
|
|
|
+import org.aspectj.lang.annotation.Pointcut;
|
|
|
+import org.springframework.core.annotation.Order;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author lqc
|
|
|
+ */
|
|
|
+@Aspect
|
|
|
+@Component
|
|
|
+@Order
|
|
|
+@RequiredArgsConstructor
|
|
|
+@Slf4j
|
|
|
+public class ParameterLogAspect {
|
|
|
+
|
|
|
+ private static ThreadLocal<Long> startTime = new ThreadLocal<Long>();
|
|
|
+ private static ThreadLocal<String> requestUrl = new ThreadLocal<String>();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 申明一个切点 里面是 execution表达式
|
|
|
+ */
|
|
|
+ @Pointcut("execution(public * com.webflux.launchadmin.mysql.controller.*.*(..))")
|
|
|
+ private void controllerAspect() {
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 请求method前打印内容
|
|
|
+ *
|
|
|
+ * @param joinPoint
|
|
|
+ */
|
|
|
+ @Before(value = "controllerAspect()")
|
|
|
+ public void methodBefore(JoinPoint joinPoint) {
|
|
|
+ try {
|
|
|
+ ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
+ HttpServletRequest request = requestAttributes.getRequest();
|
|
|
+
|
|
|
+ startTime.set(System.currentTimeMillis());
|
|
|
+ requestUrl.set(request.getRequestURL().toString());
|
|
|
+
|
|
|
+ // 打印请求内容
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ sb.append(">>请求内容>>");
|
|
|
+ sb.append("请求地址:" + request.getRequestURL().toString() + " ");
|
|
|
+ sb.append("请求方式:" + request.getMethod() + " ");
|
|
|
+ // sb.append("请求类方法:" + joinPoint.getSignature() + " ");
|
|
|
+
|
|
|
+ for (Object o : joinPoint.getArgs()) {
|
|
|
+ try {
|
|
|
+ sb.append("请求类方法参数:" + JSONUtil.toJsonStr(o) + " ");
|
|
|
+ } catch (Exception e) {
|
|
|
+ sb.append("请求类方法参数:" + o.toString() + " ");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sb.append("<<请求内容<<");
|
|
|
+
|
|
|
+ log.info(sb.toString());
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("记录参数日志发生异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 在方法执行完结后打印返回内容
|
|
|
+ *
|
|
|
+ * @param o
|
|
|
+ */
|
|
|
+ @AfterReturning(returning = "o", pointcut = "controllerAspect()")
|
|
|
+ public void methodAfterReturning(Object o) {
|
|
|
+ try {
|
|
|
+ // 打印返回结果
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ sb.append(">>返回内容>>");
|
|
|
+ sb.append("请求地址:" + requestUrl.get() + " ");
|
|
|
+ try {
|
|
|
+ sb.append("Response内容:" + JSONUtil.toJsonStr(o) + " ");
|
|
|
+ } catch (Exception e) {
|
|
|
+ sb.append("Response内容:" + o.toString() + " ");
|
|
|
+ }
|
|
|
+ sb.append("<<返回内容<<");
|
|
|
+ // 打印回车
|
|
|
+ sb.append("请求处理时间为:" + (null != startTime.get() ? (System.currentTimeMillis() - startTime.get()) : 0) + "ms \n");
|
|
|
+
|
|
|
+ if (sb.toString().contains("\"code\":200")) {
|
|
|
+ log.info(sb.toString());
|
|
|
+ } else {
|
|
|
+ log.error(sb.toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ startTime.remove();
|
|
|
+ requestUrl.remove();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("记录参数日志发生异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|