1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| @Slf4j @Aspect @Component public class TraceWatchAspect {
@Pointcut("@annotation(com.magese.api.common.annotation.TraceWatch)") public void traceWatchPointcut() { }
@Around("traceWatchPointcut()") public Object doAround(ProceedingJoinPoint joinPoint) { String className = joinPoint.getTarget().getClass().getSimpleName(); String methodName = joinPoint.getSignature().getName(); String path = sourceRoot(className, methodName, joinPoint); com.magese.api.common.annotation.TraceWatch traceWatch = ((MethodSignature) joinPoint.getSignature()) .getMethod() .getAnnotation(com.magese.api.common.annotation.TraceWatch.class); Constants.TraceLevel level = traceWatch.level(); String notes = traceWatch.notes(); TraceWatch.Watch<Object> watch = TraceWatch.run((TraceWatch.Supplier<Object, Throwable>) joinPoint::proceed); consoleLog(watch, path, level, notes); return watch.getResult(); }
private String sourceRoot(String className, String methodName, ProceedingJoinPoint joinPoint) { StringBuilder source = new StringBuilder(); source.append(className).append(".").append(methodName).append("("); Map<String, String> paramMap = new LinkedHashMap<>(); MethodSignature signature = (MethodSignature) joinPoint.getSignature(); String[] paramNames = signature.getParameterNames(); Class<?>[] classes = signature.getParameterTypes(); if (paramNames != null && classes != null && paramNames.length > 0 && classes.length > 0) { for (int i = 0; i < (Math.min(paramNames.length, classes.length)); i++) { paramMap.put(paramNames[i], classes[i].getSimpleName()); } } if (!paramMap.isEmpty()) { AtomicInteger size = new AtomicInteger(paramMap.size()); paramMap.forEach((k, v) -> source .append(v) .append(" ") .append(k) .append(size.decrementAndGet() > 0 ? ", " : "")); } source.append(")"); return source.toString(); }
private String milli2String(long milli) { return LocalDateTimeUtil.format(LocalDateTimeUtil.of(milli), "yyyy-MM-dd HH:mm:ss:SSS"); }
private void consoleLog(TraceWatch.Watch<?> watch, String classPath, Constants.TraceLevel level, String notes) { String consoleLog = StringUtils.isBlank(notes) ? "耗时监控 ==> [{}] => [耗时:{}ms] => [开始时间:{},结束时间:{}]" : "耗时监控 ==> [{}] => [{}] => [耗时:{}ms] => [开始时间:{},结束时间:{}]"; String start = milli2String(watch.getStartMs()); String end = milli2String(watch.getEndMs()); List<String> params = new ArrayList<>(); params.add(classPath); if (StringUtils.isNotBlank(notes)) params.add(notes); params.add(String.valueOf(watch.getExecMs())); params.add(start); params.add(end);
switch (level) { case TRACE: log.trace(consoleLog, params.toArray()); case DEBUG: log.debug(consoleLog, params.toArray()); break; case INFO: log.info(consoleLog, params.toArray()); break; case WARN: log.warn(consoleLog, params.toArray()); break; case ERROR: log.error(consoleLog, params.toArray()); break; } }
}
|