mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-08 12:39:05 +08:00
✨ Introducing new features. #2161 日志切面保存错误日志
This commit is contained in:
1 parent
14b5dacb64
commit
896ad75252
3 files changed
+66
-11
No files matched your search
+17
-5
@@ -23,6 +23,7 @@ import com.pig4cloud.pigx.admin.api.dto.SysLogDTO;
|
||||
import com.pig4cloud.pigx.common.core.util.KeyStrResolver;
|
||||
import com.pig4cloud.pigx.common.log.annotation.SysLog;
|
||||
import com.pig4cloud.pigx.common.log.event.SysLogEvent;
|
||||
import com.pig4cloud.pigx.common.log.util.LogTypeEnum;
|
||||
import com.pig4cloud.pigx.common.log.util.SysLogUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
@@ -30,6 +31,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
|
||||
/**
|
||||
@@ -57,11 +59,21 @@ public class SysLogAspect {
|
||||
logDTO.setTitle(sysLog.value());
|
||||
// 发送异步日志事件
|
||||
Long startTime = System.currentTimeMillis();
|
||||
Object obj = point.proceed();
|
||||
Long endTime = System.currentTimeMillis();
|
||||
logDTO.setTime(endTime - startTime);
|
||||
logDTO.setTenantId(Integer.parseInt(tenantKeyStrResolver.key()));
|
||||
publisher.publishEvent(new SysLogEvent(logDTO));
|
||||
Object obj;
|
||||
try {
|
||||
obj = point.proceed();
|
||||
}
|
||||
catch (Exception e) {
|
||||
logDTO.setType(LogTypeEnum.ERROR.getType());
|
||||
logDTO.setException(e.getMessage());
|
||||
throw e;
|
||||
}
|
||||
finally {
|
||||
Long endTime = System.currentTimeMillis();
|
||||
logDTO.setTime(endTime - startTime);
|
||||
logDTO.setTenantId(Integer.parseInt(tenantKeyStrResolver.key()));
|
||||
publisher.publishEvent(new SysLogEvent(logDTO));
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.pig4cloud.pigx.common.log.util;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author lengleng
|
||||
* @date 2021/1/13
|
||||
*/
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
+7
-6
@@ -19,21 +19,22 @@
|
||||
|
||||
package com.pig4cloud.pigx.common.log.util;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import cn.hutool.extra.servlet.ServletUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.pig4cloud.pigx.admin.api.dto.SysLogDTO;
|
||||
import com.pig4cloud.pigx.common.core.constant.CommonConstants;
|
||||
import lombok.experimental.UtilityClass;
|
||||
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 系统日志工具类
|
||||
*
|
||||
@@ -47,7 +48,7 @@ public class SysLogUtils {
|
||||
.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
|
||||
SysLogDTO sysLog = new SysLogDTO();
|
||||
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());
|
||||
@@ -60,7 +61,7 @@ public class SysLogUtils {
|
||||
public SysLogDTO getSysLog(HttpServletRequest request, String username) {
|
||||
SysLogDTO sysLog = new SysLogDTO();
|
||||
sysLog.setCreateBy(username);
|
||||
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());
|
||||
|
||||
Reference in new issue
Block a user