diff --git a/pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/annotation/SysLog.java b/pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/annotation/SysLog.java index 9248d236..d01fd1a1 100755 --- a/pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/annotation/SysLog.java +++ b/pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/annotation/SysLog.java @@ -31,6 +31,12 @@ public @interface SysLog { * 描述 * @return {String} */ - String value(); + String value() default ""; + + /** + * spel 表达式 + * @return 日志描述 + */ + String expression() default ""; } diff --git a/pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/aspect/SysLogAspect.java b/pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/aspect/SysLogAspect.java index 3270e4e1..61febbe8 100755 --- a/pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/aspect/SysLogAspect.java +++ b/pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/aspect/SysLogAspect.java @@ -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(); diff --git a/pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/util/SysLogUtils.java b/pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/util/SysLogUtils.java index 23596eef..d2d2db5d 100755 --- a/pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/util/SysLogUtils.java +++ b/pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/util/SysLogUtils.java @@ -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 返回泛型 + * @return 参数值 + */ + public T getValue(EvaluationContext context, String key, Class 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; + } }