|
@@ -1,102 +0,0 @@
|
|
|
-package com.factory.base.aspect;
|
|
|
-
|
|
|
-import java.time.Duration;
|
|
|
-import java.time.LocalDateTime;
|
|
|
-import java.util.Collection;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
-
|
|
|
-import javax.servlet.ServletRequest;
|
|
|
-import javax.servlet.ServletResponse;
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
-
|
|
|
-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.web.context.request.RequestContextHolder;
|
|
|
-import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
-import org.springframework.web.multipart.MultipartFile;
|
|
|
-
|
|
|
-import com.factory.base.util.JacksonJsonUtils;
|
|
|
-
|
|
|
-import cn.hutool.core.lang.UUID;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-
|
|
|
-/**
|
|
|
- * 通过切面 记录请求日志
|
|
|
- *
|
|
|
- * @author edz
|
|
|
- */
|
|
|
-@Slf4j
|
|
|
-@Aspect
|
|
|
-@Component
|
|
|
-public class LogAspect {
|
|
|
-
|
|
|
- @Pointcut("@within(org.springframework.web.bind.annotation.RestController)")
|
|
|
- public void logPointAround() {
|
|
|
- }
|
|
|
-
|
|
|
- @Around("logPointAround()")
|
|
|
- public Object doAroundLog(ProceedingJoinPoint thisJoinPoint) throws Throwable{
|
|
|
- Object r = null;
|
|
|
- String method = thisJoinPoint.getSignature().getName();
|
|
|
- LocalDateTime startTime = LocalDateTime.now();
|
|
|
- String uuid = UUID.fastUUID().toString();
|
|
|
- String url = null;
|
|
|
- Object userId = null;
|
|
|
- Map param = null;
|
|
|
- boolean flag = false;
|
|
|
- try {
|
|
|
- ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
- HttpServletRequest request = requestAttributes.getRequest();
|
|
|
-
|
|
|
- userId = request.getAttribute("UserId");
|
|
|
- url = request.getRequestURL().toString();
|
|
|
-
|
|
|
- param = getArgs(thisJoinPoint,((MethodSignature)thisJoinPoint.getSignature()).getParameterNames());
|
|
|
- log.info("\033[1;32m[访问地址:{}] [方法:{}] [{}] [userId:{}] [参数:{}]\033[0m",url,method,uuid,userId,param);
|
|
|
- r = thisJoinPoint.proceed ();//被代理对象执行结果
|
|
|
-
|
|
|
- } catch (Throwable e) {
|
|
|
- flag = true;
|
|
|
- throw e;//异常信息有统一异常处理器处理
|
|
|
- }
|
|
|
- log.info("\033[1;32m[访问地址:{}] [方法:{}] [{}] [执行时间:{}(豪秒)] [{}]\033[0m",url,method,uuid,Duration.between(startTime, LocalDateTime.now()).toMillis(),flag?"异常":"正常");
|
|
|
- return r;
|
|
|
- }
|
|
|
- private static String types = "java.lang.Integer,java.lang.Double,java.lang.Float,java.lang.Long,java.lang.Short,java.lang.Byte,java.lang.Boolean,java.lang.Char,java.lang.String,int,double,long,short,byte,boolean,char,float";
|
|
|
-
|
|
|
- private Map getArgs(ProceedingJoinPoint joinPoint,String[] parameterNames) {
|
|
|
- Map allArgs = new HashMap();
|
|
|
- Object[] args = joinPoint.getArgs();
|
|
|
- for (int k = 0; k < args.length; k++) {
|
|
|
- Object arg = args[k];
|
|
|
- if(arg==null) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- if(arg instanceof ServletResponse||arg instanceof ServletRequest) {//
|
|
|
- continue;
|
|
|
- }else if(arg instanceof MultipartFile) {//文件
|
|
|
- allArgs.put(parameterNames[k],((MultipartFile)arg).getOriginalFilename());
|
|
|
- }else if(arg instanceof MultipartFile[]) {//文件
|
|
|
- allArgs.put(parameterNames[k],((MultipartFile[])arg).length);
|
|
|
- }else if(arg instanceof Number||arg instanceof String||arg instanceof Character||arg instanceof Boolean) {//简单类型
|
|
|
- allArgs.put(parameterNames[k],arg);
|
|
|
- }else if(arg instanceof Collection) {//集合
|
|
|
- allArgs.put(parameterNames[k],arg);
|
|
|
- }else if(arg.getClass().isArray()) {//数组
|
|
|
- allArgs.put(parameterNames[k],arg);
|
|
|
- }else {//复杂类型
|
|
|
- Map m = JacksonJsonUtils.readObject(JacksonJsonUtils.writeObject(arg), Map.class);
|
|
|
- if(m!=null) {
|
|
|
- allArgs.putAll(m);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return allArgs;
|
|
|
- }
|
|
|
-}
|
|
|
-
|