From d63a8e31c30102cd7d25c84228d4612a88f292e2 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Tue, 23 Feb 2021 12:52:02 -0800 Subject: [PATCH] fix(logging): use thread-safe time formatter (#303) --- .../playwright/impl/LoggingSupport.java | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/LoggingSupport.java b/playwright/src/main/java/com/microsoft/playwright/impl/LoggingSupport.java index 2ff50878..5b3387c7 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/LoggingSupport.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/LoggingSupport.java @@ -16,9 +16,9 @@ package com.microsoft.playwright.impl; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.TimeZone; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; import java.util.function.Supplier; class LoggingSupport { @@ -28,12 +28,8 @@ class LoggingSupport { isEnabled = (debug != null) && debug.contains("pw:api"); } - private static final SimpleDateFormat timestampFormat; - static { - timestampFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); - timestampFormat.setTimeZone(TimeZone.getTimeZone("UTC")); - } - + private static final DateTimeFormatter timestampFormat = DateTimeFormatter.ofPattern( + "yyyy-MM-dd'T'HH:mm:ss.SSSXXX").withZone(ZoneId.of("UTC")); void withLogging(String apiName, Runnable code) { withLogging(apiName, () -> { @@ -60,7 +56,7 @@ class LoggingSupport { private void logApi(String message) { // This matches log format produced by the server. - String timestamp = timestampFormat.format(new Date()); + String timestamp = ZonedDateTime.now().format(timestampFormat); System.err.println(timestamp + " pw:api " + message); } }