1231123
This commit is contained in:
@@ -16,16 +16,20 @@ import com.ruoyi.common.utils.StringUtils;
|
|||||||
import com.ruoyi.common.utils.spring.SpringUtils;
|
import com.ruoyi.common.utils.spring.SpringUtils;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.aspectj.lang.JoinPoint;
|
import org.aspectj.lang.JoinPoint;
|
||||||
import org.aspectj.lang.annotation.AfterReturning;
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
import org.aspectj.lang.annotation.AfterThrowing;
|
import org.aspectj.lang.annotation.*;
|
||||||
import org.aspectj.lang.annotation.Aspect;
|
import org.aspectj.lang.reflect.MethodSignature;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.text.DateFormat;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.StringJoiner;
|
import java.util.StringJoiner;
|
||||||
|
|
||||||
@@ -43,16 +47,36 @@ public class LogAspect {
|
|||||||
* 排除敏感属性字段
|
* 排除敏感属性字段
|
||||||
*/
|
*/
|
||||||
public static final String[] EXCLUDE_PROPERTIES = { "password", "oldPassword", "newPassword", "confirmPassword" };
|
public static final String[] EXCLUDE_PROPERTIES = { "password", "oldPassword", "newPassword", "confirmPassword" };
|
||||||
|
// 配置织入点
|
||||||
|
@Pointcut("@annotation(com.ruoyi.common.annotation.Log)")
|
||||||
|
public void logPointCut() {
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
@Around(value = "logPointCut()")
|
||||||
|
public Object around(ProceedingJoinPoint pjp) throws Throwable {
|
||||||
|
long startTime = System.currentTimeMillis();
|
||||||
|
Log controllerLog = getAnnotationLog(pjp);
|
||||||
|
try {
|
||||||
|
Object result = pjp.proceed();
|
||||||
|
this.handleLog(pjp,controllerLog,null,result,startTime);
|
||||||
|
return result;
|
||||||
|
} catch (Exception e){
|
||||||
|
this.handleLog(pjp,controllerLog,e,null,startTime);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理完请求后执行
|
* 处理完请求后执行
|
||||||
*
|
*
|
||||||
* @param joinPoint 切点
|
* @param joinPoint 切点
|
||||||
*/
|
*/
|
||||||
@AfterReturning(pointcut = "@annotation(controllerLog)", returning = "jsonResult")
|
// @AfterReturning(pointcut = "@annotation(controllerLog)", returning = "jsonResult")
|
||||||
public void doAfterReturning(JoinPoint joinPoint, Log controllerLog, Object jsonResult) {
|
// public void doAfterReturning(JoinPoint joinPoint, Log controllerLog, Object jsonResult) {
|
||||||
handleLog(joinPoint, controllerLog, null, jsonResult);
|
// handleLog(joinPoint, controllerLog, null, jsonResult);
|
||||||
}
|
// }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 拦截异常操作
|
* 拦截异常操作
|
||||||
@@ -60,13 +84,14 @@ public class LogAspect {
|
|||||||
* @param joinPoint 切点
|
* @param joinPoint 切点
|
||||||
* @param e 异常
|
* @param e 异常
|
||||||
*/
|
*/
|
||||||
@AfterThrowing(value = "@annotation(controllerLog)", throwing = "e")
|
// @AfterThrowing(value = "@annotation(controllerLog)", throwing = "e")
|
||||||
public void doAfterThrowing(JoinPoint joinPoint, Log controllerLog, Exception e) {
|
// public void doAfterThrowing(JoinPoint joinPoint, Log controllerLog, Exception e) {
|
||||||
handleLog(joinPoint, controllerLog, e, null);
|
// handleLog(joinPoint, controllerLog, e, null);
|
||||||
}
|
// }
|
||||||
|
|
||||||
protected void handleLog(final JoinPoint joinPoint, Log controllerLog, final Exception e, Object jsonResult) {
|
protected void handleLog(final JoinPoint joinPoint, Log controllerLog, final Exception e, Object jsonResult,long startTime) {
|
||||||
try {
|
try {
|
||||||
|
long endTime = System.currentTimeMillis();
|
||||||
// *========数据库日志=========*//
|
// *========数据库日志=========*//
|
||||||
OperLogEvent operLog = new OperLogEvent();
|
OperLogEvent operLog = new OperLogEvent();
|
||||||
StringBuilder logString = new StringBuilder();
|
StringBuilder logString = new StringBuilder();
|
||||||
@@ -103,6 +128,10 @@ public class LogAspect {
|
|||||||
operLog.setErrorMsg(StringUtils.substring(e.getMessage(), 0, 2000));
|
operLog.setErrorMsg(StringUtils.substring(e.getMessage(), 0, 2000));
|
||||||
logString.append(String.format("exception=%s;",e.getMessage()));
|
logString.append(String.format("exception=%s;",e.getMessage()));
|
||||||
}
|
}
|
||||||
|
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
|
logString.append(";StartTime:").append(dateFormat.format(new Date(startTime)));
|
||||||
|
logString.append(",EndTime:").append(dateFormat.format(new Date(endTime)));
|
||||||
|
logString.append(",CostTime:").append(endTime - startTime).append("ms");
|
||||||
log.info(logString.toString());
|
log.info(logString.toString());
|
||||||
// 发布事件保存数据库
|
// 发布事件保存数据库
|
||||||
if(controllerLog.isSaveDb()){
|
if(controllerLog.isSaveDb()){
|
||||||
@@ -215,4 +244,18 @@ public class LogAspect {
|
|||||||
return o instanceof MultipartFile || o instanceof HttpServletRequest || o instanceof HttpServletResponse
|
return o instanceof MultipartFile || o instanceof HttpServletRequest || o instanceof HttpServletResponse
|
||||||
|| o instanceof BindingResult;
|
|| o instanceof BindingResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否存在注解,如果存在就获取
|
||||||
|
*/
|
||||||
|
private Log getAnnotationLog(JoinPoint joinPoint) {
|
||||||
|
// 获取方法上面的注解
|
||||||
|
Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
|
||||||
|
Log writeLogAnnotation = method.getAnnotation(Log.class);
|
||||||
|
if (writeLogAnnotation == null) {
|
||||||
|
// 获取类上的注解
|
||||||
|
writeLogAnnotation = joinPoint.getTarget().getClass().getAnnotation(Log.class);
|
||||||
|
}
|
||||||
|
return writeLogAnnotation;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user