From 7964c79a0b69fc35c9fd4a5dd5051253bcbc7c5d Mon Sep 17 00:00:00 2001 From: lihongjie0209 Date: Thu, 23 Nov 2023 09:44:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=BF=97=E9=BB=98=E8=AE=A4=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=20id=20code=20name=20=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/lihongjie/coal/aop/ControllerAop.java | 44 +++++++++++++++++-- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/src/main/java/cn/lihongjie/coal/aop/ControllerAop.java b/src/main/java/cn/lihongjie/coal/aop/ControllerAop.java index 639f804b..88ef0199 100644 --- a/src/main/java/cn/lihongjie/coal/aop/ControllerAop.java +++ b/src/main/java/cn/lihongjie/coal/aop/ControllerAop.java @@ -12,6 +12,7 @@ import jakarta.servlet.http.HttpServletRequest; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.exception.ExceptionUtils; import org.aspectj.lang.ProceedingJoinPoint; @@ -20,8 +21,13 @@ import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.expression.MethodBasedEvaluationContext; +import org.springframework.core.DefaultParameterNameDiscoverer; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.annotation.Order; +import org.springframework.expression.ExpressionParser; +import org.springframework.expression.spel.SpelParserConfiguration; +import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.context.request.RequestContextHolder; @@ -39,6 +45,8 @@ public class ControllerAop { @Autowired SessionService sessionService; @Autowired IpQueryService ipQueryService; @Autowired SysLogService sysLogService; + private final SpelParserConfiguration config; + private final ExpressionParser parser; private static void updateSysLog(Throwable e, SysLogEntity sysLogEntity) { if (sysLogEntity != null) { @@ -47,6 +55,12 @@ public class ControllerAop { } } + public ControllerAop() { + config = new SpelParserConfiguration(true, true); + + parser = new SpelExpressionParser(config); + } + @Pointcut("execution (* cn.lihongjie.coal.*.controller.*.*(..))") public void controllerMethods() {} @@ -61,7 +75,7 @@ public class ControllerAop { .getRequest(); long start = System.currentTimeMillis(); - SysLogEntity sysLogEntity = createSysLog(method, request); + SysLogEntity sysLogEntity = createSysLog(method, request, proceedingJoinPoint.getArgs()); try { return proceedingJoinPoint.proceed(); @@ -88,7 +102,7 @@ public class ControllerAop { } } - private SysLogEntity createSysLog(Method method, HttpServletRequest request) { + private SysLogEntity createSysLog(Method method, HttpServletRequest request, Object[] args) { SysLog sysLog = AnnotationUtils.findAnnotation(method, SysLog.class); SysLogEntity sysLogEntity = null; @@ -107,7 +121,7 @@ public class ControllerAop { sysLogEntity = new SysLogEntity(); sysLogEntity.setModule(module); - sysLogEntity.setMessage(sysLog.message()); + sysLogEntity.setMessage(buildMessage(sysLog, method, args, request)); sysLogEntity.setAction(sysLog.action()); sysLogEntity.setIp(RequestUtils.getIp(request)); sysLogEntity.setIpLocation(ipQueryService.query(sysLogEntity.getIp())); @@ -119,6 +133,30 @@ public class ControllerAop { return sysLogEntity; } + private String buildMessage( + SysLog sysLog, Method method, Object[] args, HttpServletRequest request) { + String message = sysLog.message(); + if (StringUtils.isEmpty(sysLog.message())) { + message = "(id?:'') + ' ' + (name?:'') + ' ' + (code?:'')"; + } + + try { + + MethodBasedEvaluationContext context = + new MethodBasedEvaluationContext( + ArrayUtils.isEmpty(args) ? null : args[0], + method, + args, + new DefaultParameterNameDiscoverer()); + context.setVariable("request", request); + Object value = parser.parseExpression(message).getValue(context); + return value + ""; + } catch (Exception e) { + log.error("解析日志表达式失败: {}", sysLog.message(), e); + return ""; + } + } + private void logException(Throwable ex, ProceedingJoinPoint proceedingJoinPoint) { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())