!231 @SysLog支持spel表达式

Merge pull request !231 from BlackR/blackr_dev
This commit is contained in:
lengleng 2022-05-20 09:58:51 +00:00 committed by Gitee
commit d609e50873
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 65 additions and 2 deletions

View File

@ -31,6 +31,12 @@ public @interface SysLog {
* 描述
* @return {String}
*/
String value();
String value() default "";
/**
* spel 表达式
* @return 日志描述
*/
String expression() default "";
}

View File

@ -16,6 +16,7 @@
package com.pig4cloud.pig.common.log.aspect;
import cn.hutool.core.util.StrUtil;
import com.pig4cloud.pig.admin.api.entity.SysLog;
import com.pig4cloud.pig.common.core.util.SpringContextHolder;
import com.pig4cloud.pig.common.log.event.SysLogEvent;
@ -26,6 +27,8 @@ import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.expression.EvaluationContext;
/**
* 操作日志使用spring event异步入库
@ -43,8 +46,23 @@ public class SysLogAspect {
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) 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);
}
}
SysLog logVo = SysLogUtils.getSysLog();
logVo.setTitle(sysLog.value());
logVo.setTitle(value);
// 发送异步日志事件
Long startTime = System.currentTimeMillis();

View File

@ -21,6 +21,11 @@ import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.HttpUtil;
import com.pig4cloud.pig.admin.api.entity.SysLog;
import lombok.experimental.UtilityClass;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
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.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
@ -31,6 +36,7 @@ import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Objects;
/**
@ -90,4 +96,37 @@ public class SysLogUtils {
return authentication.getName();
}
/**
* 获取spel 定义的参数值
*
* @param context 参数容器
* @param key key
* @param clazz 需要返回的类型
* @param <T> 返回泛型
* @return 参数值
*/
public <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 EvaluationContext getContext(Object[] arguments, Method signatureMethod) {
String[] parameterNames = new LocalVariableTableParameterNameDiscoverer().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;
}
}