|
@@ -0,0 +1,94 @@
|
|
|
+package com.xjrsoft.common.aspect;
|
|
|
+
|
|
|
+import cn.hutool.core.exceptions.ExceptionUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.xjrsoft.common.annotation.XjrLog;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.skywalking.apm.toolkit.trace.ActiveSpan;
|
|
|
+import org.apache.skywalking.apm.toolkit.trace.TraceContext;
|
|
|
+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.aspectj.lang.reflect.MethodSignature;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import javax.servlet.ServletRequest;
|
|
|
+import java.lang.reflect.Method;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Aspect
|
|
|
+@Component
|
|
|
+public class ControllerLogAspect {
|
|
|
+ public static final String BIG_STR_SUB_PATTERN = "(:s*\"[^,\":]{8192,}?\")";
|
|
|
+
|
|
|
+
|
|
|
+ * API的切点,拦截所有controller
|
|
|
+ */
|
|
|
+ @Pointcut("execution(public * com.xjrsoft.module.*.controller.*.*(..))")
|
|
|
+ public void apiPointcut() {
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 在方法执行前进行参数判断、在方法执行后记录日志
|
|
|
+ *
|
|
|
+ * @param joinPoint 入参
|
|
|
+ */
|
|
|
+ @Around("apiPointcut()")
|
|
|
+ public Object executeFaceAspect(ProceedingJoinPoint joinPoint) throws Throwable {
|
|
|
+ Object returnObj = null;
|
|
|
+ Object[] parameters = joinPoint.getArgs();
|
|
|
+ try {
|
|
|
+ returnObj = joinPoint.proceed();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("SkyWalking aspect catch exception {}", TraceContext.traceId(), e);
|
|
|
+ ActiveSpan.error(ExceptionUtil.getRootCauseMessage(e));
|
|
|
+ throw e;
|
|
|
+ } finally {
|
|
|
+ MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
|
|
+ Method method = signature.getMethod();
|
|
|
+ XjrLog xjrLog = method.getAnnotation(XjrLog.class);
|
|
|
+ if (parameters != null && parameters.length > 0) {
|
|
|
+ for (int i = 0; i < parameters.length; i++) {
|
|
|
+ try {
|
|
|
+ Object nowObj = parameters[i];
|
|
|
+ if (nowObj instanceof ServletRequest) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (xjrLog != null && xjrLog.saveRequestData()) {
|
|
|
+ ActiveSpan.tag("params[ " + i + " ]", replaceLog(JSON.toJSONString(nowObj)));
|
|
|
+ }
|
|
|
+ } catch (Exception ignore) {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (returnObj != null) {
|
|
|
+ try {
|
|
|
+ if (xjrLog != null && xjrLog.saveResponseData()) {
|
|
|
+ ActiveSpan.tag("result", replaceLog(JSON.toJSONString(returnObj)));
|
|
|
+ }
|
|
|
+ } catch (Exception ignore) {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return returnObj;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 格式化日志
|
|
|
+ *
|
|
|
+ * @param logStr 入参
|
|
|
+ * @return String
|
|
|
+ */
|
|
|
+ public static String replaceLog(String logStr) {
|
|
|
+ if (StringUtils.hasLength(logStr)) {
|
|
|
+ return logStr.replaceAll(BIG_STR_SUB_PATTERN, ":\"*\"");
|
|
|
+ }
|
|
|
+ return StrUtil.EMPTY;
|
|
|
+ }
|
|
|
+}
|