style(operalog): 格式化操作日志切面代码并增强JSON序列化容错性

This commit is contained in:
2026-01-14 20:14:12 +08:00
parent 85e4557376
commit c008daaaa1
2 changed files with 59 additions and 41 deletions
@@ -27,7 +27,6 @@ import cn.odboy.util.KitBrowserUtil;
import cn.odboy.util.KitIPUtil; import cn.odboy.util.KitIPUtil;
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSON;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import java.lang.reflect.Method;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.ProceedingJoinPoint;
@@ -36,6 +35,7 @@ import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature; import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
/** /**
* 捕捉操作日志 * 捕捉操作日志
@@ -48,44 +48,48 @@ import org.springframework.stereotype.Component;
@Component @Component
public class OperationLogAspect { public class OperationLogAspect {
@Autowired @Autowired private SystemOperationLogMapper systemOperationLogMapper;
private SystemOperationLogMapper systemOperationLogMapper;
@Around("@annotation(operationLog)") @Around("@annotation(operationLog)")
public Object operationLogCatch(ProceedingJoinPoint joinPoint, OperationLog operationLog) throws Throwable { public Object operationLogCatch(ProceedingJoinPoint joinPoint, OperationLog operationLog)
throws Throwable {
return handleLog(joinPoint, operationLog); return handleLog(joinPoint, operationLog);
} }
private Object handleLog(ProceedingJoinPoint joinPoint, OperationLog annotation) throws Throwable { private Object handleLog(ProceedingJoinPoint joinPoint, OperationLog annotation)
throws Throwable {
TimeInterval timeInterval = new TimeInterval(); TimeInterval timeInterval = new TimeInterval();
try { try {
Object result = joinPoint.proceed(); Object result = joinPoint.proceed();
long interval = timeInterval.interval(); long interval = timeInterval.interval();
SystemOperationLogTb record = getOperationLogTb(joinPoint, annotation, interval); SystemOperationLogTb record = getOperationLogTb(joinPoint, annotation, interval);
ThreadUtil.execAsync(() -> { ThreadUtil.execAsync(
try { () -> {
systemOperationLogMapper.insert(record); try {
} catch (Exception e) { systemOperationLogMapper.insert(record);
// 忽略 } catch (Exception e) {
} // 忽略
}); }
});
return result; return result;
} catch (Throwable exception) { } catch (Throwable exception) {
long interval = timeInterval.interval(); long interval = timeInterval.interval();
SystemOperationLogTb record = getOperationLogTb(joinPoint, annotation, interval); SystemOperationLogTb record = getOperationLogTb(joinPoint, annotation, interval);
record.setExceptionDetail(ExceptionUtil.stacktraceToString(exception)); record.setExceptionDetail(ExceptionUtil.stacktraceToString(exception));
ThreadUtil.execAsync(() -> { ThreadUtil.execAsync(
try { () -> {
systemOperationLogMapper.insert(record); try {
} catch (Exception e) { systemOperationLogMapper.insert(record);
// 忽略 } catch (Exception e) {
} // 忽略
}); }
});
throw exception; throw exception;
} }
} }
private SystemOperationLogTb getOperationLogTb(ProceedingJoinPoint joinPoint, OperationLog annotation, long executeTime) { private SystemOperationLogTb getOperationLogTb(
ProceedingJoinPoint joinPoint, OperationLog annotation, long executeTime) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); MethodSignature signature = (MethodSignature) joinPoint.getSignature();
String bizName = annotation.bizName(); String bizName = annotation.bizName();
if (StrUtil.isBlank(bizName)) { if (StrUtil.isBlank(bizName)) {
@@ -100,7 +104,12 @@ public class OperationLogAspect {
} }
String method = joinPoint.getTarget().getClass().getName() + "." + signature.getName() + "()"; String method = joinPoint.getTarget().getClass().getName() + "." + signature.getName() + "()";
Object[] args = joinPoint.getArgs(); Object[] args = joinPoint.getArgs();
String params = JSON.toJSONString(args); String params = "";
try {
params = JSON.toJSONString(args);
} catch (Exception e) {
// 忽略异常
}
HttpServletRequest request = KitRequestHolder.getHttpServletRequest(); HttpServletRequest request = KitRequestHolder.getHttpServletRequest();
String requestIp = KitBrowserUtil.getIp(request); String requestIp = KitBrowserUtil.getIp(request);
String browserInfo = KitBrowserUtil.getVersion(request); String browserInfo = KitBrowserUtil.getVersion(request);
@@ -48,44 +48,48 @@ import org.springframework.stereotype.Component;
@Component @Component
public class OperationLogAspect { public class OperationLogAspect {
@Autowired @Autowired private SystemOperationLogMapper systemOperationLogMapper;
private SystemOperationLogMapper systemOperationLogMapper;
@Around("@annotation(operationLog)") @Around("@annotation(operationLog)")
public Object operationLogCatch(ProceedingJoinPoint joinPoint, OperationLog operationLog) throws Throwable { public Object operationLogCatch(ProceedingJoinPoint joinPoint, OperationLog operationLog)
throws Throwable {
return handleLog(joinPoint, operationLog); return handleLog(joinPoint, operationLog);
} }
private Object handleLog(ProceedingJoinPoint joinPoint, OperationLog annotation) throws Throwable { private Object handleLog(ProceedingJoinPoint joinPoint, OperationLog annotation)
throws Throwable {
TimeInterval timeInterval = new TimeInterval(); TimeInterval timeInterval = new TimeInterval();
try { try {
Object result = joinPoint.proceed(); Object result = joinPoint.proceed();
long interval = timeInterval.interval(); long interval = timeInterval.interval();
SystemOperationLogTb record = getOperationLogTb(joinPoint, annotation, interval); SystemOperationLogTb record = getOperationLogTb(joinPoint, annotation, interval);
ThreadUtil.execAsync(() -> { ThreadUtil.execAsync(
try { () -> {
systemOperationLogMapper.insert(record); try {
} catch (Exception e) { systemOperationLogMapper.insert(record);
// 忽略 } catch (Exception e) {
} // 忽略
}); }
});
return result; return result;
} catch (Throwable exception) { } catch (Throwable exception) {
long interval = timeInterval.interval(); long interval = timeInterval.interval();
SystemOperationLogTb record = getOperationLogTb(joinPoint, annotation, interval); SystemOperationLogTb record = getOperationLogTb(joinPoint, annotation, interval);
record.setExceptionDetail(ExceptionUtil.stacktraceToString(exception)); record.setExceptionDetail(ExceptionUtil.stacktraceToString(exception));
ThreadUtil.execAsync(() -> { ThreadUtil.execAsync(
try { () -> {
systemOperationLogMapper.insert(record); try {
} catch (Exception e) { systemOperationLogMapper.insert(record);
// 忽略 } catch (Exception e) {
} // 忽略
}); }
});
throw exception; throw exception;
} }
} }
private SystemOperationLogTb getOperationLogTb(ProceedingJoinPoint joinPoint, OperationLog annotation, long executeTime) { private SystemOperationLogTb getOperationLogTb(
ProceedingJoinPoint joinPoint, OperationLog annotation, long executeTime) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); MethodSignature signature = (MethodSignature) joinPoint.getSignature();
String bizName = annotation.bizName(); String bizName = annotation.bizName();
if (StrUtil.isBlank(bizName)) { if (StrUtil.isBlank(bizName)) {
@@ -100,7 +104,12 @@ public class OperationLogAspect {
} }
String method = joinPoint.getTarget().getClass().getName() + "." + signature.getName() + "()"; String method = joinPoint.getTarget().getClass().getName() + "." + signature.getName() + "()";
Object[] args = joinPoint.getArgs(); Object[] args = joinPoint.getArgs();
String params = JSON.toJSONString(args); String params = "";
try {
params = JSON.toJSONString(args);
} catch (Exception e) {
// 忽略异常
}
HttpServletRequest request = KitRequestHolder.getHttpServletRequest(); HttpServletRequest request = KitRequestHolder.getHttpServletRequest();
String requestIp = KitBrowserUtil.getIp(request); String requestIp = KitBrowserUtil.getIp(request);
String browserInfo = KitBrowserUtil.getVersion(request); String browserInfo = KitBrowserUtil.getVersion(request);