diff --git a/pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/aspect/SysLogAspect.java b/pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/aspect/SysLogAspect.java index 9956e0cf..78f7017c 100755 --- a/pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/aspect/SysLogAspect.java +++ b/pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/aspect/SysLogAspect.java @@ -18,9 +18,10 @@ package com.pig4cloud.pig.common.log.aspect; +import com.pig4cloud.pig.admin.api.entity.SysLog; import com.pig4cloud.pig.common.core.util.SpringContextHolder; -import com.pig4cloud.pig.common.log.annotation.SysLog; import com.pig4cloud.pig.common.log.event.SysLogEvent; +import com.pig4cloud.pig.common.log.util.LogTypeEnum; import com.pig4cloud.pig.common.log.util.SysLogUtils; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; @@ -39,19 +40,32 @@ public class SysLogAspect { @Around("@annotation(sysLog)") @SneakyThrows - public Object around(ProceedingJoinPoint point, SysLog sysLog) { + public Object around(ProceedingJoinPoint point, com.pig4cloud.pig.common.log.annotation.SysLog sysLog) { String strClassName = point.getTarget().getClass().getName(); String strMethodName = point.getSignature().getName(); log.debug("[类名]:{},[方法]:{}", strClassName, strMethodName); - com.pig4cloud.pig.admin.api.entity.SysLog logVo = SysLogUtils.getSysLog(); + SysLog logVo = SysLogUtils.getSysLog(); logVo.setTitle(sysLog.value()); + // 发送异步日志事件 Long startTime = System.currentTimeMillis(); - Object obj = point.proceed(); - Long endTime = System.currentTimeMillis(); - logVo.setTime(endTime - startTime); - SpringContextHolder.publishEvent(new SysLogEvent(logVo)); + Object obj; + + try { + obj = point.proceed(); + } + catch (Exception e) { + logVo.setType(LogTypeEnum.ERROR.getType()); + logVo.setException(e.getMessage()); + throw e; + } + finally { + Long endTime = System.currentTimeMillis(); + logVo.setTime(endTime - startTime); + SpringContextHolder.publishEvent(new SysLogEvent(logVo)); + } + return obj; } diff --git a/pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/util/LogTypeEnum.java b/pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/util/LogTypeEnum.java new file mode 100644 index 00000000..925f2546 --- /dev/null +++ b/pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/util/LogTypeEnum.java @@ -0,0 +1,36 @@ +package com.pig4cloud.pig.common.log.util; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +/** + * @author lengleng + * @date 2020/7/30 + *
+ * 日志类型 + */ +@Getter +@RequiredArgsConstructor +public enum LogTypeEnum { + + /** + * 正常日志类型 + */ + NORMAL("0", "正常日志"), + + /** + * 错误日志类型 + */ + ERROR("9", "错误日志"); + + /** + * 类型 + */ + private final String type; + + /** + * 描述 + */ + private final String description; + +} diff --git a/pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/util/SysLogUtils.java b/pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/util/SysLogUtils.java index b00eee36..3438285f 100755 --- a/pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/util/SysLogUtils.java +++ b/pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/util/SysLogUtils.java @@ -22,7 +22,6 @@ import cn.hutool.core.util.URLUtil; import cn.hutool.extra.servlet.ServletUtil; import cn.hutool.http.HttpUtil; import com.pig4cloud.pig.admin.api.entity.SysLog; -import com.pig4cloud.pig.common.core.constant.CommonConstants; import lombok.experimental.UtilityClass; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; @@ -46,7 +45,7 @@ public class SysLogUtils { .requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); SysLog sysLog = new SysLog(); sysLog.setCreateBy(Objects.requireNonNull(getUsername())); - sysLog.setType(CommonConstants.STATUS_NORMAL); + sysLog.setType(LogTypeEnum.NORMAL.getType()); sysLog.setRemoteAddr(ServletUtil.getClientIP(request)); sysLog.setRequestUri(URLUtil.getPath(request.getRequestURI())); sysLog.setMethod(request.getMethod());