mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-01-25 07:46:40 +08:00
日志默认增加 id code name 记录
This commit is contained in:
@@ -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())
|
||||
|
||||
Reference in New Issue
Block a user