refactor: 日志切面ThreadLocal优化

This commit is contained in:
fit2-zhao 2023-07-28 18:38:42 +08:00 committed by fit2-zhao
parent f7f380d0b3
commit c97d176ad0
1 changed files with 14 additions and 14 deletions

View File

@ -10,6 +10,7 @@ import io.metersphere.sdk.util.SessionUtils;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.JoinPoint;
@ -47,20 +48,20 @@ public class OperationLogAspect {
private final StandardReflectionParameterNameDiscoverer private final StandardReflectionParameterNameDiscoverer
discoverer = new StandardReflectionParameterNameDiscoverer(); discoverer = new StandardReflectionParameterNameDiscoverer();
private final String ID = "id"; private final static String ID = "id";
@Resource @Resource
private ApplicationContext applicationContext; private ApplicationContext applicationContext;
@Resource @Resource
private OperationLogService operationLogService; private OperationLogService operationLogService;
// 批量变更前后内容 // 批量变更前后内容
private ThreadLocal<List<LogDTO>> beforeValues = new ThreadLocal<>(); private final ThreadLocal<List<LogDTO>> beforeValues = new ThreadLocal<>();
private ThreadLocal<String> localUser = new ThreadLocal<>(); private final ThreadLocal<String> localUser = new ThreadLocal<>();
private ThreadLocal<String> localOrganizationId = new ThreadLocal<>(); private final ThreadLocal<String> localOrganizationId = new ThreadLocal<>();
private ThreadLocal<String> localProjectId = new ThreadLocal<>(); private final ThreadLocal<String> localProjectId = new ThreadLocal<>();
// 此方法随时补充类型需要在内容变更前执行的类型都可以加入 // 此方法随时补充类型需要在内容变更前执行的类型都可以加入
private final OperationLogType[] beforeMethodNames = new OperationLogType[]{OperationLogType.UPDATE, OperationLogType.DELETE}; private final OperationLogType[] beforeMethodNames = new OperationLogType[]{OperationLogType.UPDATE, OperationLogType.DELETE};
@ -103,11 +104,11 @@ public class OperationLogAspect {
//将参数纳入Spring管理 //将参数纳入Spring管理
EvaluationContext context = new StandardEvaluationContext(); EvaluationContext context = new StandardEvaluationContext();
for (int len = 0; len < params.length; len++) { for (int len = 0; len < Objects.requireNonNull(params).length; len++) {
context.setVariable(params[len], args[len]); context.setVariable(params[len], args[len]);
} }
boolean isNext = false; boolean isNext = false;
for (Class clazz : msLog.msClass()) { for (Class<?> clazz : msLog.msClass()) {
context.setVariable("msClass", applicationContext.getBean(clazz)); context.setVariable("msClass", applicationContext.getBean(clazz));
isNext = true; isNext = true;
} }
@ -179,8 +180,7 @@ public class OperationLogAspect {
if (obj instanceof List<?>) { if (obj instanceof List<?>) {
mergeLists(beforeValues.get(), (List<LogDTO>) obj); mergeLists(beforeValues.get(), (List<LogDTO>) obj);
} else if (obj instanceof LogDTO) { } else if (obj instanceof LogDTO log) {
LogDTO log = (LogDTO) obj;
if (CollectionUtils.isNotEmpty(beforeValues.get())) { if (CollectionUtils.isNotEmpty(beforeValues.get())) {
beforeValues.get().get(0).setModifiedValue(log.getOriginalValue()); beforeValues.get().get(0).setModifiedValue(log.getOriginalValue());
} else { } else {
@ -201,7 +201,7 @@ public class OperationLogAspect {
if (result != null) { if (result != null) {
String resultStr = JSON.toJSONString(result); String resultStr = JSON.toJSONString(result);
Map object = JSON.parseMap(resultStr); Map object = JSON.parseMap(resultStr);
if (object != null && object.containsKey(ID)) { if (MapUtils.isNotEmpty(object) && object.containsKey(ID)) {
Object nameValue = object.get(ID); Object nameValue = object.get(ID);
if (ObjectUtils.isNotEmpty(nameValue)) { if (ObjectUtils.isNotEmpty(nameValue)) {
return nameValue.toString(); return nameValue.toString();
@ -209,7 +209,7 @@ public class OperationLogAspect {
} }
} }
} catch (Exception e) { } catch (Exception e) {
LogUtils.error("未获取到响应Id"); LogUtils.error("未获取到响应资源Id");
} }
return null; return null;
} }
@ -255,10 +255,10 @@ public class OperationLogAspect {
String[] params = discoverer.getParameterNames(method); String[] params = discoverer.getParameterNames(method);
//将参数纳入Spring管理 //将参数纳入Spring管理
EvaluationContext context = new StandardEvaluationContext(); EvaluationContext context = new StandardEvaluationContext();
for (int len = 0; len < params.length; len++) { for (int len = 0; len < Objects.requireNonNull(params).length; len++) {
context.setVariable(params[len], args[len]); context.setVariable(params[len], args[len]);
} }
for (Class clazz : msLog.msClass()) { for (Class<?> clazz : msLog.msClass()) {
context.setVariable("msClass", applicationContext.getBean(clazz)); context.setVariable("msClass", applicationContext.getBean(clazz));
} }
// 需要后置再次执行的方法 // 需要后置再次执行的方法