mirror of
				https://github.com/continew-org/continew-starter.git
				synced 2025-10-31 22:57:19 +08:00 
			
		
		
		
	fix(log): 修复接口耗时统计始终为0ms问题
This commit is contained in:
		| @@ -18,7 +18,6 @@ package top.continew.starter.log.core.model; | |||||||
|  |  | ||||||
| import top.continew.starter.log.core.enums.Include; | import top.continew.starter.log.core.enums.Include; | ||||||
|  |  | ||||||
| import java.time.Clock; |  | ||||||
| import java.time.Duration; | import java.time.Duration; | ||||||
| import java.time.Instant; | import java.time.Instant; | ||||||
| import java.util.Set; | import java.util.Set; | ||||||
| @@ -78,7 +77,7 @@ public class LogRecord { | |||||||
|      * @return 日志记录器 |      * @return 日志记录器 | ||||||
|      */ |      */ | ||||||
|     public static Started start(RecordableHttpRequest request) { |     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   请求信息 |      * @param request   请求信息 | ||||||
|      * @return 日志记录器 |      * @return 日志记录器 | ||||||
|      */ |      */ | ||||||
|     public static Started start(Clock timestamp, RecordableHttpRequest request) { |     public static Started start(Instant timestamp, RecordableHttpRequest request) { | ||||||
|         return new Started(timestamp, request); |         return new Started(timestamp, request); | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -101,8 +100,8 @@ public class LogRecord { | |||||||
|  |  | ||||||
|         private final RecordableHttpRequest request; |         private final RecordableHttpRequest request; | ||||||
|  |  | ||||||
|         private Started(Clock clock, RecordableHttpRequest request) { |         private Started(Instant timestamp, RecordableHttpRequest request) { | ||||||
|             this.timestamp = Instant.now(clock); |             this.timestamp = timestamp; | ||||||
|             this.request = request; |             this.request = request; | ||||||
|         } |         } | ||||||
|  |  | ||||||
| @@ -114,10 +113,10 @@ public class LogRecord { | |||||||
|          * @param includes 包含信息 |          * @param includes 包含信息 | ||||||
|          * @return 日志记录 |          * @return 日志记录 | ||||||
|          */ |          */ | ||||||
|         public LogRecord finish(Clock clock, RecordableHttpResponse response, Set<Include> includes) { |         public LogRecord finish(Instant timestamp, RecordableHttpResponse response, Set<Include> includes) { | ||||||
|             LogRequest logRequest = new LogRequest(this.request, includes); |             LogRequest logRequest = new LogRequest(this.request, includes); | ||||||
|             LogResponse logResponse = new LogResponse(response, 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); |             return new LogRecord(this.timestamp, logRequest, logResponse, duration); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -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.core.model.LogRecord; | ||||||
| import top.continew.starter.log.interceptor.autoconfigure.LogProperties; | import top.continew.starter.log.interceptor.autoconfigure.LogProperties; | ||||||
|  |  | ||||||
| import java.time.Clock; |  | ||||||
| import java.time.Duration; | import java.time.Duration; | ||||||
| import java.time.Instant; | import java.time.Instant; | ||||||
| import java.util.HashSet; | import java.util.HashSet; | ||||||
| @@ -51,7 +50,7 @@ public class LogInterceptor implements HandlerInterceptor { | |||||||
|     private static final Logger log = LoggerFactory.getLogger(LogInterceptor.class); |     private static final Logger log = LoggerFactory.getLogger(LogInterceptor.class); | ||||||
|     private final LogDao logDao; |     private final LogDao logDao; | ||||||
|     private final LogProperties logProperties; |     private final LogProperties logProperties; | ||||||
|     private final TransmittableThreadLocal<Clock> timeTtl = new TransmittableThreadLocal<>(); |     private final TransmittableThreadLocal<Instant> timeTtl = new TransmittableThreadLocal<>(); | ||||||
|     private final TransmittableThreadLocal<LogRecord.Started> logTtl = new TransmittableThreadLocal<>(); |     private final TransmittableThreadLocal<LogRecord.Started> logTtl = new TransmittableThreadLocal<>(); | ||||||
|  |  | ||||||
|     public LogInterceptor(LogDao logDao, LogProperties logProperties) { |     public LogInterceptor(LogDao logDao, LogProperties logProperties) { | ||||||
| @@ -63,7 +62,7 @@ public class LogInterceptor implements HandlerInterceptor { | |||||||
|     public boolean preHandle(@NonNull HttpServletRequest request, |     public boolean preHandle(@NonNull HttpServletRequest request, | ||||||
|                              @NonNull HttpServletResponse response, |                              @NonNull HttpServletResponse response, | ||||||
|                              @NonNull Object handler) { |                              @NonNull Object handler) { | ||||||
|         Clock startTime = Clock.systemUTC(); |         Instant startTime = Instant.now(); | ||||||
|         if (Boolean.TRUE.equals(logProperties.getIsPrint())) { |         if (Boolean.TRUE.equals(logProperties.getIsPrint())) { | ||||||
|             log.info("[{}] {}", request.getMethod(), request.getRequestURI()); |             log.info("[{}] {}", request.getMethod(), request.getRequestURI()); | ||||||
|             timeTtl.set(startTime); |             timeTtl.set(startTime); | ||||||
| @@ -81,9 +80,9 @@ public class LogInterceptor implements HandlerInterceptor { | |||||||
|                                 @NonNull Object handler, |                                 @NonNull Object handler, | ||||||
|                                 Exception e) { |                                 Exception e) { | ||||||
|         try { |         try { | ||||||
|             Clock endTime = Clock.systemUTC(); |             Instant endTime = Instant.now(); | ||||||
|             if (Boolean.TRUE.equals(logProperties.getIsPrint())) { |             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 |                 log.info("[{}] {} {} {}ms", request.getMethod(), request.getRequestURI(), response | ||||||
|                     .getStatus(), timeTaken.toMillis()); |                     .getStatus(), timeTaken.toMillis()); | ||||||
|             } |             } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 梓陌
					梓陌