From 4caf0a0db69779a5ea409ec7e01c4044817afc94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=93=E9=99=8C?= <958142070@qq.com> Date: Tue, 3 Sep 2024 06:19:54 +0000 Subject: [PATCH] =?UTF-8?q?fix(log):=20=E4=BF=AE=E5=A4=8D=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E8=80=97=E6=97=B6=E7=BB=9F=E8=AE=A1=E5=A7=8B=E7=BB=88?= =?UTF-8?q?=E4=B8=BA0ms=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../continew/starter/log/core/model/LogRecord.java | 13 ++++++------- .../log/interceptor/handler/LogInterceptor.java | 9 ++++----- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/model/LogRecord.java b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/model/LogRecord.java index 93197d77..9df4a49f 100644 --- a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/model/LogRecord.java +++ b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/model/LogRecord.java @@ -18,7 +18,6 @@ package top.continew.starter.log.core.model; import top.continew.starter.log.core.enums.Include; -import java.time.Clock; import java.time.Duration; import java.time.Instant; import java.util.Set; @@ -78,7 +77,7 @@ public class LogRecord { * @return 日志记录器 */ public static Started start(RecordableHttpRequest request) { - return start(Clock.systemUTC(), request); + return start(Instant.now(), request); } /** @@ -88,7 +87,7 @@ public class LogRecord { * @param request 请求信息 * @return 日志记录器 */ - public static Started start(Clock timestamp, RecordableHttpRequest request) { + public static Started start(Instant timestamp, RecordableHttpRequest request) { return new Started(timestamp, request); } @@ -101,8 +100,8 @@ public class LogRecord { private final RecordableHttpRequest request; - private Started(Clock clock, RecordableHttpRequest request) { - this.timestamp = Instant.now(clock); + private Started(Instant timestamp, RecordableHttpRequest request) { + this.timestamp = timestamp; this.request = request; } @@ -114,10 +113,10 @@ public class LogRecord { * @param includes 包含信息 * @return 日志记录 */ - public LogRecord finish(Clock clock, RecordableHttpResponse response, Set includes) { + public LogRecord finish(Instant timestamp, RecordableHttpResponse response, Set includes) { LogRequest logRequest = new LogRequest(this.request, includes); LogResponse logResponse = new LogResponse(response, includes); - Duration duration = Duration.between(this.timestamp, Instant.now(clock)); + Duration duration = Duration.between(this.timestamp, timestamp); return new LogRecord(this.timestamp, logRequest, logResponse, duration); } } diff --git a/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/handler/LogInterceptor.java b/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/handler/LogInterceptor.java index a63d151d..9a0c27ad 100644 --- a/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/handler/LogInterceptor.java +++ b/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/handler/LogInterceptor.java @@ -34,7 +34,6 @@ import top.continew.starter.log.core.enums.Include; import top.continew.starter.log.core.model.LogRecord; import top.continew.starter.log.interceptor.autoconfigure.LogProperties; -import java.time.Clock; import java.time.Duration; import java.time.Instant; import java.util.HashSet; @@ -51,7 +50,7 @@ public class LogInterceptor implements HandlerInterceptor { private static final Logger log = LoggerFactory.getLogger(LogInterceptor.class); private final LogDao logDao; private final LogProperties logProperties; - private final TransmittableThreadLocal timeTtl = new TransmittableThreadLocal<>(); + private final TransmittableThreadLocal timeTtl = new TransmittableThreadLocal<>(); private final TransmittableThreadLocal logTtl = new TransmittableThreadLocal<>(); public LogInterceptor(LogDao logDao, LogProperties logProperties) { @@ -63,7 +62,7 @@ public class LogInterceptor implements HandlerInterceptor { public boolean preHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler) { - Clock startTime = Clock.systemUTC(); + Instant startTime = Instant.now(); if (Boolean.TRUE.equals(logProperties.getIsPrint())) { log.info("[{}] {}", request.getMethod(), request.getRequestURI()); timeTtl.set(startTime); @@ -81,9 +80,9 @@ public class LogInterceptor implements HandlerInterceptor { @NonNull Object handler, Exception e) { try { - Clock endTime = Clock.systemUTC(); + Instant endTime = Instant.now(); if (Boolean.TRUE.equals(logProperties.getIsPrint())) { - Duration timeTaken = Duration.between(Instant.now(timeTtl.get()), Instant.now(endTime)); + Duration timeTaken = Duration.between(timeTtl.get(), endTime); log.info("[{}] {} {} {}ms", request.getMethod(), request.getRequestURI(), response .getStatus(), timeTaken.toMillis()); }