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 com.alibaba.fastjson2.JSON;
import io.swagger.annotations.ApiOperation;
import java.lang.reflect.Method;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
@@ -36,6 +35,7 @@ import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
/**
* 捕捉操作日志
@@ -48,21 +48,23 @@ import org.springframework.stereotype.Component;
@Component
public class OperationLogAspect {
@Autowired
private SystemOperationLogMapper systemOperationLogMapper;
@Autowired private SystemOperationLogMapper systemOperationLogMapper;
@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);
}
private Object handleLog(ProceedingJoinPoint joinPoint, OperationLog annotation) throws Throwable {
private Object handleLog(ProceedingJoinPoint joinPoint, OperationLog annotation)
throws Throwable {
TimeInterval timeInterval = new TimeInterval();
try {
Object result = joinPoint.proceed();
long interval = timeInterval.interval();
SystemOperationLogTb record = getOperationLogTb(joinPoint, annotation, interval);
ThreadUtil.execAsync(() -> {
ThreadUtil.execAsync(
() -> {
try {
systemOperationLogMapper.insert(record);
} catch (Exception e) {
@@ -74,7 +76,8 @@ public class OperationLogAspect {
long interval = timeInterval.interval();
SystemOperationLogTb record = getOperationLogTb(joinPoint, annotation, interval);
record.setExceptionDetail(ExceptionUtil.stacktraceToString(exception));
ThreadUtil.execAsync(() -> {
ThreadUtil.execAsync(
() -> {
try {
systemOperationLogMapper.insert(record);
} catch (Exception e) {
@@ -85,7 +88,8 @@ public class OperationLogAspect {
}
}
private SystemOperationLogTb getOperationLogTb(ProceedingJoinPoint joinPoint, OperationLog annotation, long executeTime) {
private SystemOperationLogTb getOperationLogTb(
ProceedingJoinPoint joinPoint, OperationLog annotation, long executeTime) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
String bizName = annotation.bizName();
if (StrUtil.isBlank(bizName)) {
@@ -100,7 +104,12 @@ public class OperationLogAspect {
}
String method = joinPoint.getTarget().getClass().getName() + "." + signature.getName() + "()";
Object[] args = joinPoint.getArgs();
String params = JSON.toJSONString(args);
String params = "";
try {
params = JSON.toJSONString(args);
} catch (Exception e) {
// 忽略异常
}
HttpServletRequest request = KitRequestHolder.getHttpServletRequest();
String requestIp = KitBrowserUtil.getIp(request);
String browserInfo = KitBrowserUtil.getVersion(request);
@@ -48,21 +48,23 @@ import org.springframework.stereotype.Component;
@Component
public class OperationLogAspect {
@Autowired
private SystemOperationLogMapper systemOperationLogMapper;
@Autowired private SystemOperationLogMapper systemOperationLogMapper;
@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);
}
private Object handleLog(ProceedingJoinPoint joinPoint, OperationLog annotation) throws Throwable {
private Object handleLog(ProceedingJoinPoint joinPoint, OperationLog annotation)
throws Throwable {
TimeInterval timeInterval = new TimeInterval();
try {
Object result = joinPoint.proceed();
long interval = timeInterval.interval();
SystemOperationLogTb record = getOperationLogTb(joinPoint, annotation, interval);
ThreadUtil.execAsync(() -> {
ThreadUtil.execAsync(
() -> {
try {
systemOperationLogMapper.insert(record);
} catch (Exception e) {
@@ -74,7 +76,8 @@ public class OperationLogAspect {
long interval = timeInterval.interval();
SystemOperationLogTb record = getOperationLogTb(joinPoint, annotation, interval);
record.setExceptionDetail(ExceptionUtil.stacktraceToString(exception));
ThreadUtil.execAsync(() -> {
ThreadUtil.execAsync(
() -> {
try {
systemOperationLogMapper.insert(record);
} catch (Exception e) {
@@ -85,7 +88,8 @@ public class OperationLogAspect {
}
}
private SystemOperationLogTb getOperationLogTb(ProceedingJoinPoint joinPoint, OperationLog annotation, long executeTime) {
private SystemOperationLogTb getOperationLogTb(
ProceedingJoinPoint joinPoint, OperationLog annotation, long executeTime) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
String bizName = annotation.bizName();
if (StrUtil.isBlank(bizName)) {
@@ -100,7 +104,12 @@ public class OperationLogAspect {
}
String method = joinPoint.getTarget().getClass().getName() + "." + signature.getName() + "()";
Object[] args = joinPoint.getArgs();
String params = JSON.toJSONString(args);
String params = "";
try {
params = JSON.toJSONString(args);
} catch (Exception e) {
// 忽略异常
}
HttpServletRequest request = KitRequestHolder.getHttpServletRequest();
String requestIp = KitBrowserUtil.getIp(request);
String browserInfo = KitBrowserUtil.getVersion(request);