|
@@ -1,10 +1,13 @@
|
|
package com.usky.common.log.aspect;//package com.ruoyi.common.log.aspect;
|
|
package com.usky.common.log.aspect;//package com.ruoyi.common.log.aspect;
|
|
|
|
|
|
import java.util.Collection;
|
|
import java.util.Collection;
|
|
|
|
+import java.util.Date;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
import java.util.Objects;
|
|
import java.util.Objects;
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
+import javax.xml.crypto.Data;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.usky.common.core.util.IpUtils;
|
|
import com.usky.common.core.util.IpUtils;
|
|
@@ -16,9 +19,8 @@ import com.usky.common.log.service.AsyncLogService;
|
|
import com.usky.common.security.utils.SecurityUtils;
|
|
import com.usky.common.security.utils.SecurityUtils;
|
|
import com.usky.system.domain.SysOperLogVO;
|
|
import com.usky.system.domain.SysOperLogVO;
|
|
import org.aspectj.lang.JoinPoint;
|
|
import org.aspectj.lang.JoinPoint;
|
|
-import org.aspectj.lang.annotation.AfterReturning;
|
|
|
|
-import org.aspectj.lang.annotation.AfterThrowing;
|
|
|
|
-import org.aspectj.lang.annotation.Aspect;
|
|
|
|
|
|
+import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
|
+import org.aspectj.lang.annotation.*;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -34,56 +36,49 @@ import org.springframework.web.multipart.MultipartFile;
|
|
*/
|
|
*/
|
|
@Aspect
|
|
@Aspect
|
|
@Component
|
|
@Component
|
|
-public class LogAspect
|
|
|
|
-{
|
|
|
|
|
|
+public class LogAspect {
|
|
private static final Logger log = LoggerFactory.getLogger(LogAspect.class);
|
|
private static final Logger log = LoggerFactory.getLogger(LogAspect.class);
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
private AsyncLogService asyncLogService;
|
|
private AsyncLogService asyncLogService;
|
|
|
|
|
|
- /**
|
|
|
|
- * 处理完请求后执行
|
|
|
|
- *
|
|
|
|
- * @param joinPoint 切点
|
|
|
|
- */
|
|
|
|
- @AfterReturning(pointcut = "@annotation(controllerLog)", returning = "jsonResult")
|
|
|
|
- public void doAfterReturning(JoinPoint joinPoint, Log controllerLog, Object jsonResult)
|
|
|
|
- {
|
|
|
|
- handleLog(joinPoint, controllerLog, null, jsonResult);
|
|
|
|
|
|
+ @Pointcut("@annotation(controllerLog)")
|
|
|
|
+ public void logPointcut(Log controllerLog) {
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * 拦截异常操作
|
|
|
|
- *
|
|
|
|
- * @param joinPoint 切点
|
|
|
|
- * @param e 异常
|
|
|
|
- */
|
|
|
|
- @AfterThrowing(value = "@annotation(controllerLog)", throwing = "e")
|
|
|
|
- public void doAfterThrowing(JoinPoint joinPoint, Log controllerLog, Exception e)
|
|
|
|
- {
|
|
|
|
- handleLog(joinPoint, controllerLog, e, null);
|
|
|
|
|
|
+ @Around("logPointcut(controllerLog)")
|
|
|
|
+ public Object aroundLog(ProceedingJoinPoint joinPoint, Log controllerLog) throws Throwable {
|
|
|
|
+ Date startTime = new Date();
|
|
|
|
+ Object result = null;
|
|
|
|
+ Exception exception = null;
|
|
|
|
+ try {
|
|
|
|
+ result = joinPoint.proceed();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ exception = e;
|
|
|
|
+ throw e;
|
|
|
|
+ } finally {
|
|
|
|
+ Date endTime = new Date();
|
|
|
|
+ long consumingTime = TimeUnit.MILLISECONDS.convert(endTime.getTime() - startTime.getTime(), TimeUnit.MILLISECONDS);
|
|
|
|
+ handleLog(joinPoint, controllerLog, exception, result, startTime, endTime, consumingTime);
|
|
|
|
+ }
|
|
|
|
+ return result;
|
|
}
|
|
}
|
|
|
|
|
|
- protected void handleLog(final JoinPoint joinPoint, Log controllerLog, final Exception e, Object jsonResult)
|
|
|
|
- {
|
|
|
|
- try
|
|
|
|
- {
|
|
|
|
|
|
+ protected void handleLog(final JoinPoint joinPoint, Log controllerLog, final Exception e, Object jsonResult, Date startTime, Date endTime, long consumingTime) {
|
|
|
|
+ try {
|
|
// *========数据库日志=========*//
|
|
// *========数据库日志=========*//
|
|
SysOperLogVO operLog = new SysOperLogVO();
|
|
SysOperLogVO operLog = new SysOperLogVO();
|
|
- operLog.setStatus(BusinessStatus.SUCCESS.ordinal());
|
|
|
|
|
|
+ operLog.setStatus(e == null ? BusinessStatus.SUCCESS.ordinal() : BusinessStatus.FAIL.ordinal());
|
|
// 请求的地址
|
|
// 请求的地址
|
|
String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
|
|
String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
|
|
operLog.setOperIp(ip);
|
|
operLog.setOperIp(ip);
|
|
operLog.setOperUrl(ServletUtils.getRequest().getRequestURI());
|
|
operLog.setOperUrl(ServletUtils.getRequest().getRequestURI());
|
|
String username = SecurityUtils.getUsername();
|
|
String username = SecurityUtils.getUsername();
|
|
- if (StringUtils.isNotBlank(username))
|
|
|
|
- {
|
|
|
|
|
|
+ if (StringUtils.isNotBlank(username)) {
|
|
operLog.setOperName(username);
|
|
operLog.setOperName(username);
|
|
}
|
|
}
|
|
|
|
|
|
- if (e != null)
|
|
|
|
- {
|
|
|
|
- operLog.setStatus(BusinessStatus.FAIL.ordinal());
|
|
|
|
|
|
+ if (e != null) {
|
|
operLog.setErrorMsg(StringUtils.substring(e.getMessage(), 0, 2000));
|
|
operLog.setErrorMsg(StringUtils.substring(e.getMessage(), 0, 2000));
|
|
}
|
|
}
|
|
// 设置方法名称
|
|
// 设置方法名称
|
|
@@ -94,11 +89,17 @@ public class LogAspect
|
|
operLog.setRequestMethod(ServletUtils.getRequest().getMethod());
|
|
operLog.setRequestMethod(ServletUtils.getRequest().getMethod());
|
|
// 处理设置注解上的参数
|
|
// 处理设置注解上的参数
|
|
getControllerMethodDescription(joinPoint, controllerLog, operLog, jsonResult);
|
|
getControllerMethodDescription(joinPoint, controllerLog, operLog, jsonResult);
|
|
|
|
+ // 记录开始时间、结束时间与耗时
|
|
|
|
+ operLog.setStartTime(startTime);
|
|
|
|
+ operLog.setEndTime(endTime);
|
|
|
|
+
|
|
|
|
+ // 将耗时转换为带有单位"ms"的字符串
|
|
|
|
+ String consumingTimeWithUnit = consumingTime + "ms";
|
|
|
|
+ operLog.setConsumingTimeWithUnit(consumingTimeWithUnit); // 存储带有单位的字符串
|
|
|
|
+
|
|
// 保存数据库
|
|
// 保存数据库
|
|
asyncLogService.saveSysLog(operLog);
|
|
asyncLogService.saveSysLog(operLog);
|
|
- }
|
|
|
|
- catch (Exception exp)
|
|
|
|
- {
|
|
|
|
|
|
+ } catch (Exception exp) {
|
|
// 记录本地异常日志
|
|
// 记录本地异常日志
|
|
log.error("==前置通知异常==");
|
|
log.error("==前置通知异常==");
|
|
log.error("异常信息:{}", exp.getMessage());
|
|
log.error("异常信息:{}", exp.getMessage());
|
|
@@ -113,8 +114,7 @@ public class LogAspect
|
|
* @param operLog 操作日志
|
|
* @param operLog 操作日志
|
|
* @throws Exception
|
|
* @throws Exception
|
|
*/
|
|
*/
|
|
- public void getControllerMethodDescription(JoinPoint joinPoint, Log log, SysOperLogVO operLog, Object jsonResult) throws Exception
|
|
|
|
- {
|
|
|
|
|
|
+ public void getControllerMethodDescription(JoinPoint joinPoint, Log log, SysOperLogVO operLog, Object jsonResult) throws Exception {
|
|
// 设置action动作
|
|
// 设置action动作
|
|
operLog.setBusinessType(log.businessType().ordinal());
|
|
operLog.setBusinessType(log.businessType().ordinal());
|
|
// 设置标题
|
|
// 设置标题
|
|
@@ -122,14 +122,12 @@ public class LogAspect
|
|
// 设置操作人类别
|
|
// 设置操作人类别
|
|
operLog.setOperatorType(log.operatorType().ordinal());
|
|
operLog.setOperatorType(log.operatorType().ordinal());
|
|
// 是否需要保存request,参数和值
|
|
// 是否需要保存request,参数和值
|
|
- if (log.isSaveRequestData())
|
|
|
|
- {
|
|
|
|
|
|
+ if (log.isSaveRequestData()) {
|
|
// 获取参数的信息,传入到数据库中。
|
|
// 获取参数的信息,传入到数据库中。
|
|
setRequestValue(joinPoint, operLog);
|
|
setRequestValue(joinPoint, operLog);
|
|
}
|
|
}
|
|
// 是否需要保存response,参数和值
|
|
// 是否需要保存response,参数和值
|
|
- if (log.isSaveResponseData() && Objects.nonNull(jsonResult))
|
|
|
|
- {
|
|
|
|
|
|
+ if (log.isSaveResponseData() && Objects.nonNull(jsonResult)) {
|
|
operLog.setJsonResult(StringUtils.substring(JSON.toJSONString(jsonResult), 0, 2000));
|
|
operLog.setJsonResult(StringUtils.substring(JSON.toJSONString(jsonResult), 0, 2000));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -140,11 +138,9 @@ public class LogAspect
|
|
* @param operLog 操作日志
|
|
* @param operLog 操作日志
|
|
* @throws Exception 异常
|
|
* @throws Exception 异常
|
|
*/
|
|
*/
|
|
- private void setRequestValue(JoinPoint joinPoint, SysOperLogVO operLog) throws Exception
|
|
|
|
- {
|
|
|
|
|
|
+ private void setRequestValue(JoinPoint joinPoint, SysOperLogVO operLog) throws Exception {
|
|
String requestMethod = operLog.getRequestMethod();
|
|
String requestMethod = operLog.getRequestMethod();
|
|
- if (HttpMethod.PUT.name().equals(requestMethod) || HttpMethod.POST.name().equals(requestMethod))
|
|
|
|
- {
|
|
|
|
|
|
+ if (HttpMethod.PUT.name().equals(requestMethod) || HttpMethod.POST.name().equals(requestMethod)) {
|
|
String params = argsArrayToString(joinPoint.getArgs());
|
|
String params = argsArrayToString(joinPoint.getArgs());
|
|
operLog.setOperParam(StringUtils.substring(params, 0, 2000));
|
|
operLog.setOperParam(StringUtils.substring(params, 0, 2000));
|
|
}
|
|
}
|
|
@@ -153,22 +149,15 @@ public class LogAspect
|
|
/**
|
|
/**
|
|
* 参数拼装
|
|
* 参数拼装
|
|
*/
|
|
*/
|
|
- private String argsArrayToString(Object[] paramsArray)
|
|
|
|
- {
|
|
|
|
|
|
+ private String argsArrayToString(Object[] paramsArray) {
|
|
String params = "";
|
|
String params = "";
|
|
- if (paramsArray != null && paramsArray.length > 0)
|
|
|
|
- {
|
|
|
|
- for (Object o : paramsArray)
|
|
|
|
- {
|
|
|
|
- if (Objects.nonNull(o) && !isFilterObject(o))
|
|
|
|
- {
|
|
|
|
- try
|
|
|
|
- {
|
|
|
|
|
|
+ if (paramsArray != null && paramsArray.length > 0) {
|
|
|
|
+ for (Object o : paramsArray) {
|
|
|
|
+ if (Objects.nonNull(o) && !isFilterObject(o)) {
|
|
|
|
+ try {
|
|
Object jsonObj = JSON.toJSON(o);
|
|
Object jsonObj = JSON.toJSON(o);
|
|
params += jsonObj.toString() + " ";
|
|
params += jsonObj.toString() + " ";
|
|
- }
|
|
|
|
- catch (Exception e)
|
|
|
|
- {
|
|
|
|
|
|
+ } catch (Exception e) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -183,26 +172,18 @@ public class LogAspect
|
|
* @return 如果是需要过滤的对象,则返回true;否则返回false。
|
|
* @return 如果是需要过滤的对象,则返回true;否则返回false。
|
|
*/
|
|
*/
|
|
@SuppressWarnings("rawtypes")
|
|
@SuppressWarnings("rawtypes")
|
|
- public boolean isFilterObject(final Object o)
|
|
|
|
- {
|
|
|
|
|
|
+ public boolean isFilterObject(final Object o) {
|
|
Class<?> clazz = o.getClass();
|
|
Class<?> clazz = o.getClass();
|
|
- if (clazz.isArray())
|
|
|
|
- {
|
|
|
|
|
|
+ if (clazz.isArray()) {
|
|
return clazz.getComponentType().isAssignableFrom(MultipartFile.class);
|
|
return clazz.getComponentType().isAssignableFrom(MultipartFile.class);
|
|
- }
|
|
|
|
- else if (Collection.class.isAssignableFrom(clazz))
|
|
|
|
- {
|
|
|
|
|
|
+ } else if (Collection.class.isAssignableFrom(clazz)) {
|
|
Collection collection = (Collection) o;
|
|
Collection collection = (Collection) o;
|
|
- for (Object value : collection)
|
|
|
|
- {
|
|
|
|
|
|
+ for (Object value : collection) {
|
|
return value instanceof MultipartFile;
|
|
return value instanceof MultipartFile;
|
|
}
|
|
}
|
|
- }
|
|
|
|
- else if (Map.class.isAssignableFrom(clazz))
|
|
|
|
- {
|
|
|
|
|
|
+ } else if (Map.class.isAssignableFrom(clazz)) {
|
|
Map map = (Map) o;
|
|
Map map = (Map) o;
|
|
- for (Object value : map.entrySet())
|
|
|
|
- {
|
|
|
|
|
|
+ for (Object value : map.entrySet()) {
|
|
Map.Entry entry = (Map.Entry) value;
|
|
Map.Entry entry = (Map.Entry) value;
|
|
return entry.getValue() instanceof MultipartFile;
|
|
return entry.getValue() instanceof MultipartFile;
|
|
}
|
|
}
|
|
@@ -210,4 +191,4 @@ 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;
|
|
}
|
|
}
|
|
-}
|
|
|
|
|
|
+}
|