|
@@ -0,0 +1,117 @@
|
|
|
|
+package cn.iselab.mooctest.site.common.aspect;
|
|
|
|
+
|
|
|
|
+import com.google.common.base.Stopwatch;
|
|
|
|
+import com.google.gson.ExclusionStrategy;
|
|
|
|
+import com.google.gson.FieldAttributes;
|
|
|
|
+import com.google.gson.Gson;
|
|
|
|
+import com.google.gson.GsonBuilder;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.aspectj.lang.JoinPoint;
|
|
|
|
+import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
|
+import org.aspectj.lang.annotation.*;
|
|
|
|
+import org.aspectj.lang.reflect.MethodSignature;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+
|
|
|
|
+import java.lang.reflect.Method;
|
|
|
|
+import java.util.StringJoiner;
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @program: mooctest-site
|
|
|
|
+ * @mail: menduo96@gmail.com
|
|
|
|
+ * @author: menduo
|
|
|
|
+ * @create: 2019-10-15 14:18
|
|
|
|
+ **/
|
|
|
|
+@Aspect
|
|
|
|
+@Component
|
|
|
|
+@Slf4j
|
|
|
|
+public class LogAspect {
|
|
|
|
+
|
|
|
|
+ @Pointcut("execution(public * cn.iselab.mooctest.site.web..*(..))")
|
|
|
|
+ private void logicPointCut(){}
|
|
|
|
+
|
|
|
|
+ @Pointcut("execution(public * cn.iselab.mooctest.site.web.data..*(..))")
|
|
|
|
+ private void wrapperPointCut(){}
|
|
|
|
+
|
|
|
|
+ @Pointcut("execution(public * cn.iselab.mooctest.site.web.SessionCounter.*(..))")
|
|
|
|
+ private void sessionCounter(){}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Around("logicPointCut() && !wrapperPointCut() && !sessionCounter()")
|
|
|
|
+ public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
|
|
|
|
+ beforeMethod(joinPoint);
|
|
|
|
+ Stopwatch stopwatch = Stopwatch.createStarted();
|
|
|
|
+ Object result = null;
|
|
|
|
+ try {
|
|
|
|
+ result = joinPoint.proceed();
|
|
|
|
+ stopwatch.stop();
|
|
|
|
+ return result;
|
|
|
|
+ } finally {
|
|
|
|
+ afterMethod(joinPoint,stopwatch,result);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private void beforeMethod(JoinPoint joinPoint) {
|
|
|
|
+ MethodSignature signature = (MethodSignature)joinPoint.getSignature();
|
|
|
|
+ Method method = signature.getMethod();
|
|
|
|
+ Object[] args = joinPoint.getArgs();
|
|
|
|
+ String[] names = signature.getParameterNames();
|
|
|
|
+ Class[] classes = signature.getParameterTypes();
|
|
|
|
+ StringJoiner stringJoiner = new StringJoiner(",","[","]");
|
|
|
|
+ for (int i = 0; i < names.length; i++) {
|
|
|
|
+ if(isString(args[i]) || isPrimitive(args[i]) || classes[i].getName().contains("cn.iselab")) {
|
|
|
|
+ stringJoiner.add(names[i] + ":"+ createFilterGson().toJson(args[i]));
|
|
|
|
+ } else {
|
|
|
|
+ stringJoiner.add(names[i]);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ log.info("方法名: [" + method.getName() + "]"
|
|
|
|
+ + "--参数列表: " + stringJoiner.toString());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private void afterMethod(JoinPoint joinPoint,Stopwatch stopwatch,Object result) {
|
|
|
|
+ MethodSignature signature = (MethodSignature)joinPoint.getSignature();
|
|
|
|
+ Method method = signature.getMethod();
|
|
|
|
+ log.info("方法名: [" + method.getName() + "]"
|
|
|
|
+ + "--返回值: [" + object2String(result) + "]"
|
|
|
|
+ + "--执行时间: [" + stopwatch.elapsed(TimeUnit.MILLISECONDS) + "ms].");
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private String object2String(Object result) {
|
|
|
|
+ if(result ==null) {
|
|
|
|
+ return null;
|
|
|
|
+ } else {
|
|
|
|
+ return result.toString();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static boolean isPrimitive(Object obj) {
|
|
|
|
+ try {
|
|
|
|
+ return ((Class<?>) obj.getClass().getField("TYPE").get(null)).isPrimitive();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static boolean isString(Object obj) {
|
|
|
|
+ return obj instanceof String;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private Gson createFilterGson() {
|
|
|
|
+ return new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() {
|
|
|
|
+ @Override
|
|
|
|
+ public boolean shouldSkipField(FieldAttributes f) {
|
|
|
|
+ return f.getName().contains("password");
|
|
|
|
+ }
|
|
|
|
+ @Override
|
|
|
|
+ public boolean shouldSkipClass(Class<?> aClass) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }).create();
|
|
|
|
+ }
|
|
|
|
+}
|