|
| 1 | +package top.cadecode.uniboot.common.plugin.log.aspect; |
| 2 | + |
| 3 | +import cn.hutool.core.exceptions.ExceptionUtil; |
| 4 | +import cn.hutool.core.util.ObjectUtil; |
| 5 | +import com.dtp.core.thread.DtpExecutor; |
| 6 | +import lombok.*; |
| 7 | +import lombok.extern.slf4j.Slf4j; |
| 8 | +import org.aspectj.lang.ProceedingJoinPoint; |
| 9 | +import org.aspectj.lang.annotation.Around; |
| 10 | +import org.aspectj.lang.annotation.Aspect; |
| 11 | +import org.aspectj.lang.annotation.Pointcut; |
| 12 | +import org.aspectj.lang.reflect.MethodSignature; |
| 13 | +import org.springframework.stereotype.Component; |
| 14 | +import org.springframework.web.context.request.RequestContextHolder; |
| 15 | +import org.springframework.web.context.request.ServletRequestAttributes; |
| 16 | +import top.cadecode.uniboot.common.core.util.JacksonUtil; |
| 17 | +import top.cadecode.uniboot.common.plugin.log.annotation.ApiLogger; |
| 18 | +import top.cadecode.uniboot.common.plugin.log.handler.AbstractApiLogHandler; |
| 19 | + |
| 20 | +import javax.servlet.http.HttpServletRequest; |
| 21 | + |
| 22 | +/** |
| 23 | + * 请求响应信息日志 AOP 类 |
| 24 | + * |
| 25 | + * @author Cade Li |
| 26 | + * @date 2021/12/4 |
| 27 | + */ |
| 28 | +@Slf4j |
| 29 | +@RequiredArgsConstructor |
| 30 | +@Aspect |
| 31 | +@Component |
| 32 | +public class ApiLoggerAspect { |
| 33 | + |
| 34 | + private final DtpExecutor asyncExecutor; |
| 35 | + |
| 36 | + private final AbstractApiLogHandler apiLogHandler; |
| 37 | + |
| 38 | + @Pointcut("@within(top.cadecode.uniboot.common.plugin.log.annotation.ApiLogger) " + |
| 39 | + "|| @annotation(top.cadecode.uniboot.common.plugin.log.annotation.ApiLogger)") |
| 40 | + public void pointCut() { |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * 环绕通知 |
| 46 | + * |
| 47 | + * @param point 切入点 |
| 48 | + * @return 原方法返回值 |
| 49 | + * @throws Throwable 异常信息 |
| 50 | + */ |
| 51 | + @Around("pointCut()") |
| 52 | + public Object around(ProceedingJoinPoint point) throws Throwable { |
| 53 | + // 根据注解判断是否开启 |
| 54 | + ApiLogger apiLogger = getApiLogger(point); |
| 55 | + if (!apiLogger.value()) { |
| 56 | + return point.proceed(); |
| 57 | + } |
| 58 | + // 统计耗时 |
| 59 | + long startTime = System.currentTimeMillis(); |
| 60 | + Object result = null; |
| 61 | + Throwable throwable = null; |
| 62 | + try { |
| 63 | + result = point.proceed(); |
| 64 | + return result; |
| 65 | + } catch (Throwable e) { |
| 66 | + throwable = e; |
| 67 | + throw e; |
| 68 | + } finally { |
| 69 | + handleLogger(point, apiLogger, result, throwable, System.currentTimeMillis() - startTime); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * 获取 ApiLogger 注解 |
| 75 | + */ |
| 76 | + private ApiLogger getApiLogger(ProceedingJoinPoint point) { |
| 77 | + MethodSignature methodSignature = (MethodSignature) point.getSignature(); |
| 78 | + ApiLogger apiLogger = methodSignature.getMethod().getAnnotation(ApiLogger.class); |
| 79 | + if (apiLogger == null) { |
| 80 | + apiLogger = methodSignature.getMethod() |
| 81 | + .getDeclaringClass() |
| 82 | + .getAnnotation(ApiLogger.class); |
| 83 | + } |
| 84 | + return apiLogger; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * 获取请求、结果、异常等信息,构造日志 |
| 89 | + */ |
| 90 | + public void handleLogger(ProceedingJoinPoint point, ApiLogger apiLogger, Object result, Throwable throwable, Long timeCost) { |
| 91 | + // 解析请求对象 |
| 92 | + ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
| 93 | + if (ObjectUtil.isNull(attributes)) { |
| 94 | + return; |
| 95 | + } |
| 96 | + try { |
| 97 | + String resultStr = null; |
| 98 | + boolean exceptional = false; |
| 99 | + if (ObjectUtil.isNotNull(throwable)) { |
| 100 | + exceptional = true; |
| 101 | + resultStr = ExceptionUtil.stacktraceToString(throwable); |
| 102 | + } else { |
| 103 | + try { |
| 104 | + resultStr = JacksonUtil.toJson(result); |
| 105 | + } catch (Exception e) { |
| 106 | + resultStr = ExceptionUtil.stacktraceToString(e); |
| 107 | + log.warn("API log [{}]: request result to json fail", apiLogger.type().getType(), e); |
| 108 | + } |
| 109 | + } |
| 110 | + BaseLogInfo baseLogInfo = BaseLogInfo.builder().apiLogger(apiLogger).request(attributes.getRequest()) |
| 111 | + .resultStr(resultStr).timeCost(timeCost).exceptional(exceptional).build(); |
| 112 | + Object logObj = apiLogHandler.generateLog(point, baseLogInfo); |
| 113 | + // 打印日志 |
| 114 | + log.info("API log [{}]: {}", apiLogger.type().getType(), JacksonUtil.toJson(logObj)); |
| 115 | + // 持久化 异步 |
| 116 | + asyncExecutor.execute(() -> { |
| 117 | + try { |
| 118 | + apiLogHandler.save(apiLogger, logObj); |
| 119 | + } catch (Exception e) { |
| 120 | + log.warn("API log [{}]: save async fail", apiLogger.type().getType(), e); |
| 121 | + } |
| 122 | + }); |
| 123 | + } catch (Exception e) { |
| 124 | + log.warn("API log [{}]: handle logger fail", apiLogger.type().getType(), e); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @Data |
| 129 | + @AllArgsConstructor |
| 130 | + @NoArgsConstructor |
| 131 | + @Builder |
| 132 | + public static class BaseLogInfo { |
| 133 | + private ApiLogger apiLogger; |
| 134 | + private Boolean exceptional; |
| 135 | + private HttpServletRequest request; |
| 136 | + private String resultStr; |
| 137 | + private Long timeCost; |
| 138 | + } |
| 139 | +} |
0 commit comments