|
5 | 5 | import cn.hutool.extra.servlet.ServletUtil; |
6 | 6 | import cn.hutool.http.useragent.UserAgent; |
7 | 7 | import cn.hutool.http.useragent.UserAgentUtil; |
| 8 | +import com.dtp.core.thread.DtpExecutor; |
8 | 9 | import io.swagger.annotations.ApiOperation; |
9 | 10 | import lombok.RequiredArgsConstructor; |
10 | 11 | import lombok.extern.slf4j.Slf4j; |
|
15 | 16 | import org.aspectj.lang.annotation.Aspect; |
16 | 17 | import org.aspectj.lang.annotation.Pointcut; |
17 | 18 | import org.aspectj.lang.reflect.MethodSignature; |
| 19 | +import org.springframework.scheduling.annotation.Async; |
18 | 20 | import org.springframework.stereotype.Component; |
19 | 21 | import org.springframework.util.ObjectUtils; |
20 | 22 | import org.springframework.web.context.request.RequestContextHolder; |
|
46 | 48 | @Component |
47 | 49 | public class ApiLoggerAspect { |
48 | 50 |
|
| 51 | + private final DtpExecutor asyncExecutor; |
| 52 | + |
49 | 53 | private final SysLogService logService; |
50 | 54 |
|
51 | 55 | @Pointcut("@within(top.cadecode.uniboot.common.annotation.ApiLogger) " + |
@@ -121,7 +125,16 @@ public void handleLogger(ProceedingJoinPoint point, ApiLogger apiLogger, Object |
121 | 125 | } |
122 | 126 | } |
123 | 127 | 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 | + }); |
125 | 138 | } catch (Exception e) { |
126 | 139 | log.warn("API log [{}]: handle logger fail", apiLogger.type().getType(), e); |
127 | 140 | } |
@@ -206,10 +219,8 @@ public static Map<String, Object> getRequestParams(JoinPoint joinPoint, ApiLogge |
206 | 219 | /** |
207 | 220 | * 持久化日志 |
208 | 221 | */ |
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) { |
213 | 224 | if (!apiLogger.enableSave()) { |
214 | 225 | return; |
215 | 226 | } |
|
0 commit comments