Skip to content

Commit 947df05

Browse files
committed
feat: ApiLogger 入库使用异步
1 parent a8942e6 commit 947df05

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

framework/src/main/java/top/cadecode/uniboot/framework/aspect/ApiLoggerAspect.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import cn.hutool.extra.servlet.ServletUtil;
66
import cn.hutool.http.useragent.UserAgent;
77
import cn.hutool.http.useragent.UserAgentUtil;
8+
import com.dtp.core.thread.DtpExecutor;
89
import io.swagger.annotations.ApiOperation;
910
import lombok.RequiredArgsConstructor;
1011
import lombok.extern.slf4j.Slf4j;
@@ -15,6 +16,7 @@
1516
import org.aspectj.lang.annotation.Aspect;
1617
import org.aspectj.lang.annotation.Pointcut;
1718
import org.aspectj.lang.reflect.MethodSignature;
19+
import org.springframework.scheduling.annotation.Async;
1820
import org.springframework.stereotype.Component;
1921
import org.springframework.util.ObjectUtils;
2022
import org.springframework.web.context.request.RequestContextHolder;
@@ -46,6 +48,8 @@
4648
@Component
4749
public class ApiLoggerAspect {
4850

51+
private final DtpExecutor asyncExecutor;
52+
4953
private final SysLogService logService;
5054

5155
@Pointcut("@within(top.cadecode.uniboot.common.annotation.ApiLogger) " +
@@ -121,7 +125,16 @@ public void handleLogger(ProceedingJoinPoint point, ApiLogger apiLogger, Object
121125
}
122126
}
123127
SysLogInfoDto logInfo = generateLogInfo(point, apiLogger, exceptional, attributes.getRequest(), resultStr, timeCost);
124-
logAndSave(apiLogger, logInfo);
128+
// 打印日志
129+
log.info("API log [{}]: {}", apiLogger.type().getType(), JacksonUtil.toJson(logInfo));
130+
// 持久化 异步
131+
asyncExecutor.execute(() -> {
132+
try {
133+
save(apiLogger, logInfo);
134+
} catch (Exception e) {
135+
log.warn("API log [{}]: save async fail", apiLogger.type().getType(), e);
136+
}
137+
});
125138
} catch (Exception e) {
126139
log.warn("API log [{}]: handle logger fail", apiLogger.type().getType(), e);
127140
}
@@ -206,10 +219,8 @@ public static Map<String, Object> getRequestParams(JoinPoint joinPoint, ApiLogge
206219
/**
207220
* 持久化日志
208221
*/
209-
private void logAndSave(ApiLogger apiLogger, SysLogInfoDto logInfo) {
210-
// 打印日志
211-
log.info("API log [{}]: {}", apiLogger.type().getType(), JacksonUtil.toJson(logInfo));
212-
// 持久化
222+
@Async
223+
public void save(ApiLogger apiLogger, SysLogInfoDto logInfo) {
213224
if (!apiLogger.enableSave()) {
214225
return;
215226
}

0 commit comments

Comments
 (0)