fix: 修复日志功能
This commit is contained in:
@@ -18,6 +18,23 @@
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!--安全依赖获取上下文信息-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-oauth2-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-extra</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-http</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.meowrain</groupId>
|
||||
<artifactId>aioj-backend-common-core</artifactId>
|
||||
|
||||
@@ -3,6 +3,8 @@ package cn.meowrain.aioj.backend.framework.log;
|
||||
import cn.meowrain.aioj.backend.framework.log.aspect.SysLogAspect;
|
||||
import cn.meowrain.aioj.backend.framework.log.config.AIOJLogPropertiesConfiguration;
|
||||
import cn.meowrain.aioj.backend.framework.log.event.SysLogListener;
|
||||
import cn.meowrain.aioj.backend.upms.api.feign.RemoteLogService;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -19,6 +21,7 @@ public class LogAutoConfiguration {
|
||||
* 创建并返回SysLogListener的Bean实例
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnBean(RemoteLogService.class)
|
||||
public SysLogListener sysLogListener(AIOJLogPropertiesConfiguration logProperties,
|
||||
RemoteLogService remoteLogService) {
|
||||
return new SysLogListener(remoteLogService, logProperties);
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
package cn.meowrain.aioj.backend.framework.log.aspect;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.meowrain.aioj.backend.framework.core.utils.SpringContextHolder;
|
||||
import cn.meowrain.aioj.backend.framework.log.annotation.SysLog;
|
||||
import cn.meowrain.aioj.backend.framework.log.enums.LogTypeEnum;
|
||||
import cn.meowrain.aioj.backend.framework.log.event.SysLogEvent;
|
||||
import cn.meowrain.aioj.backend.framework.log.event.SysLogEventSource;
|
||||
import cn.meowrain.aioj.backend.framework.log.utils.SysLogUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
@@ -15,19 +20,60 @@ import org.springframework.expression.EvaluationContext;
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class SysLogAspect {
|
||||
|
||||
/**
|
||||
* 环绕通知方法,用于处理系统日志记录
|
||||
* @param point 连接点对象
|
||||
* @param sysLog 系统日志注解
|
||||
* @return 目标方法执行结果
|
||||
* @throws Throwable 目标方法执行可能抛出的异常
|
||||
*/
|
||||
@Around("@annotation(sysLog)")
|
||||
public Object around(ProceedingJoinPoint joinPoint, SysLog sysLog) throws Throwable {
|
||||
String strClassName = joinPoint.getTarget().getClass().getName();
|
||||
String strMethodName = joinPoint.getSignature().getName();
|
||||
@SneakyThrows
|
||||
public Object around(ProceedingJoinPoint point,SysLog sysLog) {
|
||||
String strClassName = point.getTarget().getClass().getName();
|
||||
String strMethodName = point.getSignature().getName();
|
||||
log.debug("[类名]:{},[方法]:{}", strClassName, strMethodName);
|
||||
|
||||
String value = sysLog.value();
|
||||
String expression = sysLog.expression();
|
||||
// 当前表达式存在 SPEL,会覆盖 value 的值
|
||||
if (StrUtil.isNotBlank(expression)) {
|
||||
// 解析SPEL
|
||||
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||
EvaluationContext context = SysLogUtils.getContext(joinPoint.getArgs(), signature.getMethod());
|
||||
MethodSignature signature = (MethodSignature) point.getSignature();
|
||||
EvaluationContext context = SysLogUtils.getContext(point.getArgs(), signature.getMethod());
|
||||
try {
|
||||
value = SysLogUtils.getValue(context, expression, String.class);
|
||||
}
|
||||
catch (Exception e) {
|
||||
// SPEL 表达式异常,获取 value 的值
|
||||
log.error("@SysLog 解析SPEL {} 异常", expression);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SysLogEventSource logVo = SysLogUtils.getSysLog();
|
||||
logVo.setTitle(value);
|
||||
// 获取请求body参数
|
||||
if (StrUtil.isBlank(logVo.getParams())) {
|
||||
logVo.setBody(point.getArgs());
|
||||
}
|
||||
// 发送异步日志事件
|
||||
Long startTime = System.currentTimeMillis();
|
||||
Object obj;
|
||||
|
||||
try {
|
||||
obj = point.proceed();
|
||||
}
|
||||
catch (Exception e) {
|
||||
logVo.setLogType(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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package cn.meowrain.aioj.backend.framework.log.event;
|
||||
|
||||
import cn.meowrain.aioj.backend.framework.log.annotation.SysLog;
|
||||
|
||||
import cn.meowrain.aioj.backend.upms.api.entity.SysLog;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
import java.io.Serial;
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
package cn.meowrain.aioj.backend.framework.log.event;
|
||||
|
||||
import cn.meowrain.aioj.backend.upms.api.entity.SysLog;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 系统
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
public class SysLogEventSource extends SysLogEvent implements Serializable {
|
||||
public class SysLogEventSource extends SysLog {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@@ -1,11 +1,101 @@
|
||||
package cn.meowrain.aioj.backend.framework.log.utils;
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import cn.hutool.extra.servlet.JakartaServletUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.meowrain.aioj.backend.framework.core.utils.SpringContextHolder;
|
||||
import cn.meowrain.aioj.backend.framework.log.config.AIOJLogPropertiesConfiguration;
|
||||
import cn.meowrain.aioj.backend.framework.log.enums.LogTypeEnum;
|
||||
import cn.meowrain.aioj.backend.framework.log.event.SysLogEventSource;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.core.StandardReflectionParameterNameDiscoverer;
|
||||
import org.springframework.expression.EvaluationContext;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public final class SysLogUtils {
|
||||
|
||||
public static SysLogEventSource getSysLog() {
|
||||
|
||||
/**
|
||||
* 获取系统日志事件源
|
||||
* @return 系统日志事件源对象
|
||||
*/
|
||||
public static SysLogEventSource getSysLog() {
|
||||
HttpServletRequest request = ((ServletRequestAttributes) Objects
|
||||
.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
|
||||
SysLogEventSource sysLog = new SysLogEventSource();
|
||||
sysLog.setLogType(LogTypeEnum.NORMAL.getType());
|
||||
sysLog.setRequestUri(URLUtil.getPath(request.getRequestURI()));
|
||||
sysLog.setMethod(request.getMethod());
|
||||
sysLog.setRemoteAddr(JakartaServletUtil.getClientIP(request));
|
||||
sysLog.setUserAgent(request.getHeader(HttpHeaders.USER_AGENT));
|
||||
sysLog.setCreateBy(getUsername());
|
||||
sysLog.setServiceId(SpringUtil.getProperty("spring.application.name"));
|
||||
|
||||
// get 参数脱敏
|
||||
AIOJLogPropertiesConfiguration logProperties = SpringContextHolder.getBean(AIOJLogPropertiesConfiguration.class);
|
||||
Map<String, String[]> paramsMap = MapUtil.removeAny(new HashMap<>(request.getParameterMap()),
|
||||
ArrayUtil.toArray(logProperties.getExcludeFields(), String.class));
|
||||
sysLog.setParams(HttpUtil.toParams(paramsMap));
|
||||
return sysLog;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户名称
|
||||
* @return username
|
||||
*/
|
||||
private static String getUsername() {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (authentication == null) {
|
||||
return null;
|
||||
}
|
||||
return authentication.getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取spel 定义的参数值
|
||||
* @param context 参数容器
|
||||
* @param key key
|
||||
* @param clazz 需要返回的类型
|
||||
* @param <T> 返回泛型
|
||||
* @return 参数值
|
||||
*/
|
||||
public static <T> T getValue(EvaluationContext context, String key, Class<T> clazz) {
|
||||
SpelExpressionParser spelExpressionParser = new SpelExpressionParser();
|
||||
Expression expression = spelExpressionParser.parseExpression(key);
|
||||
return expression.getValue(context, clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取参数容器
|
||||
* @param arguments 方法的参数列表
|
||||
* @param signatureMethod 被执行的方法体
|
||||
* @return 装载参数的容器
|
||||
*/
|
||||
public static EvaluationContext getContext(Object[] arguments, Method signatureMethod) {
|
||||
String[] parameterNames = new StandardReflectionParameterNameDiscoverer().getParameterNames(signatureMethod);
|
||||
EvaluationContext context = new StandardEvaluationContext();
|
||||
if (parameterNames == null) {
|
||||
return context;
|
||||
}
|
||||
for (int i = 0; i < arguments.length; i++) {
|
||||
context.setVariable(parameterNames[i], arguments[i]);
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user