refactor: 日志完善
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
package com.youlai.system.plugin.syslog.annotation;
|
||||
|
||||
import com.youlai.system.enums.LogTypeEnum;
|
||||
import com.youlai.system.enums.LogModuleEnum;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@@ -17,7 +17,7 @@ public @interface LogAnnotation {
|
||||
|
||||
String value() default "";
|
||||
|
||||
LogTypeEnum logType() default LogTypeEnum.OPERATION;
|
||||
LogModuleEnum module() ;
|
||||
|
||||
|
||||
}
|
||||
@@ -2,6 +2,12 @@ package com.youlai.system.plugin.syslog.aspect;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.date.TimeInterval;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.http.useragent.Browser;
|
||||
import cn.hutool.http.useragent.OS;
|
||||
import cn.hutool.http.useragent.UserAgent;
|
||||
import cn.hutool.http.useragent.UserAgentUtil;
|
||||
import com.youlai.system.common.constant.SecurityConstants;
|
||||
import com.youlai.system.common.util.IPUtils;
|
||||
import com.youlai.system.model.entity.SysLog;
|
||||
import com.youlai.system.plugin.syslog.annotation.LogAnnotation;
|
||||
@@ -9,6 +15,7 @@ import com.youlai.system.security.util.SecurityUtils;
|
||||
import com.youlai.system.service.SysLogService;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
@@ -24,6 +31,7 @@ import org.springframework.stereotype.Component;
|
||||
@Aspect
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class LogAspect {
|
||||
|
||||
private final SysLogService logService;
|
||||
@@ -35,19 +43,46 @@ public class LogAspect {
|
||||
|
||||
@Around("logPointcut() && @annotation(logAnnotation)")
|
||||
public Object logExecutionTime(ProceedingJoinPoint joinPoint, LogAnnotation logAnnotation) throws Throwable {
|
||||
String requestURI = request.getRequestURI();
|
||||
|
||||
Long userId = null;
|
||||
// 非登录请求获取用户ID,登录请求在登录成功后(joinPoint.proceed())获取用户ID
|
||||
if (!SecurityConstants.LOGIN_PATH.equals(requestURI)) {
|
||||
userId = SecurityUtils.getUserId();
|
||||
}
|
||||
|
||||
TimeInterval timer = DateUtil.timer();
|
||||
Object proceed = joinPoint.proceed();
|
||||
long executionTime =timer.interval();
|
||||
long executionTime = timer.interval();
|
||||
|
||||
// 创建日志对象
|
||||
SysLog log = new SysLog();
|
||||
log.setType(logAnnotation.logType().getValue());
|
||||
log.setTitle(logAnnotation.value());
|
||||
log.setRequestUri(request.getRequestURI());
|
||||
log.setIp(IPUtils.getIpAddr(request));
|
||||
log.setExecutionTime(executionTime);
|
||||
log.setCreateBy(SecurityUtils.getUserId());
|
||||
|
||||
log.setModule(logAnnotation.module());
|
||||
log.setContent(logAnnotation.value());
|
||||
log.setRequestUri(requestURI);
|
||||
// 登录方法需要在登录成功后获取用户ID
|
||||
if (userId == null) {
|
||||
userId = SecurityUtils.getUserId();
|
||||
}
|
||||
log.setCreateBy(userId);
|
||||
String ipAddr = IPUtils.getIpAddr(request);
|
||||
if (StrUtil.isNotBlank(ipAddr)) {
|
||||
log.setIp(ipAddr);
|
||||
String region = IPUtils.getRegion(ipAddr);
|
||||
log.setRegion(region);
|
||||
}
|
||||
log.setExecutionTime(executionTime);
|
||||
// 方法名
|
||||
log.setMethod(joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
|
||||
// 获取浏览器和终端系统信息
|
||||
String userAgentString = request.getHeader("User-Agent");
|
||||
UserAgent userAgent = UserAgentUtil.parse(userAgentString);
|
||||
// 系统信息
|
||||
log.setOs(userAgent.getOs().getName());
|
||||
// 浏览器信息
|
||||
String browserInfo = userAgent.getBrowser().getName() + " " + userAgent.getBrowser().getVersion(userAgentString);
|
||||
log.setBrowser(browserInfo);
|
||||
// 保存日志到数据库
|
||||
logService.save(log);
|
||||
|
||||
@@ -55,8 +90,4 @@ public class LogAspect {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user