♻️ Refactoring code. 错误日志记录到数据库

This commit is contained in:
冷冷 2020-07-30 16:12:23 +08:00
parent b899f2993a
commit 4151ea5e4b
3 changed files with 58 additions and 9 deletions

View File

@ -18,9 +18,10 @@
package com.pig4cloud.pig.common.log.aspect; 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.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.event.SysLogEvent;
import com.pig4cloud.pig.common.log.util.LogTypeEnum;
import com.pig4cloud.pig.common.log.util.SysLogUtils; import com.pig4cloud.pig.common.log.util.SysLogUtils;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -39,19 +40,32 @@ public class SysLogAspect {
@Around("@annotation(sysLog)") @Around("@annotation(sysLog)")
@SneakyThrows @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 strClassName = point.getTarget().getClass().getName();
String strMethodName = point.getSignature().getName(); String strMethodName = point.getSignature().getName();
log.debug("[类名]:{},[方法]:{}", strClassName, strMethodName); log.debug("[类名]:{},[方法]:{}", strClassName, strMethodName);
com.pig4cloud.pig.admin.api.entity.SysLog logVo = SysLogUtils.getSysLog(); SysLog logVo = SysLogUtils.getSysLog();
logVo.setTitle(sysLog.value()); logVo.setTitle(sysLog.value());
// 发送异步日志事件 // 发送异步日志事件
Long startTime = System.currentTimeMillis(); Long startTime = System.currentTimeMillis();
Object obj = point.proceed(); Object obj;
Long endTime = System.currentTimeMillis();
logVo.setTime(endTime - startTime); try {
SpringContextHolder.publishEvent(new SysLogEvent(logVo)); 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; return obj;
} }

View File

@ -0,0 +1,36 @@
package com.pig4cloud.pig.common.log.util;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
/**
* @author lengleng
* @date 2020/7/30
* <p>
* 日志类型
*/
@Getter
@RequiredArgsConstructor
public enum LogTypeEnum {
/**
* 正常日志类型
*/
NORMAL("0", "正常日志"),
/**
* 错误日志类型
*/
ERROR("9", "错误日志");
/**
* 类型
*/
private final String type;
/**
* 描述
*/
private final String description;
}

View File

@ -22,7 +22,6 @@ import cn.hutool.core.util.URLUtil;
import cn.hutool.extra.servlet.ServletUtil; import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.HttpUtil; import cn.hutool.http.HttpUtil;
import com.pig4cloud.pig.admin.api.entity.SysLog; import com.pig4cloud.pig.admin.api.entity.SysLog;
import com.pig4cloud.pig.common.core.constant.CommonConstants;
import lombok.experimental.UtilityClass; import lombok.experimental.UtilityClass;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
@ -46,7 +45,7 @@ public class SysLogUtils {
.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); .requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
SysLog sysLog = new SysLog(); SysLog sysLog = new SysLog();
sysLog.setCreateBy(Objects.requireNonNull(getUsername())); sysLog.setCreateBy(Objects.requireNonNull(getUsername()));
sysLog.setType(CommonConstants.STATUS_NORMAL); sysLog.setType(LogTypeEnum.NORMAL.getType());
sysLog.setRemoteAddr(ServletUtil.getClientIP(request)); sysLog.setRemoteAddr(ServletUtil.getClientIP(request));
sysLog.setRequestUri(URLUtil.getPath(request.getRequestURI())); sysLog.setRequestUri(URLUtil.getPath(request.getRequestURI()));
sysLog.setMethod(request.getMethod()); sysLog.setMethod(request.getMethod());