Skip to content

Commit f9f3a0d

Browse files
committed
[java] Replacing SimpleDateFormat with a more modern class DateTimeFormatter
1 parent 50f7404 commit f9f3a0d

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

java/client/src/org/openqa/selenium/logging/LogEntry.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717

1818
package org.openqa.selenium.logging;
1919

20-
import org.openqa.selenium.Beta;
21-
22-
import java.text.SimpleDateFormat;
23-
import java.util.Date;
20+
import java.time.Instant;
21+
import java.time.format.DateTimeFormatter;
2422
import java.util.HashMap;
2523
import java.util.Map;
2624
import java.util.logging.Level;
@@ -30,9 +28,6 @@
3028
*/
3129
public class LogEntry {
3230

33-
private static final ThreadLocal<SimpleDateFormat> DATE_FORMAT =
34-
ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"));
35-
3631
private final Level level;
3732
private final long timestamp;
3833
private final String message;
@@ -77,11 +72,11 @@ public String getMessage() {
7772

7873
@Override
7974
public String toString() {
80-
return String.format("[%s] [%s] %s",
81-
DATE_FORMAT.get().format(new Date(timestamp)), level, message);
75+
return String.format(
76+
"[%s] [%s] %s",
77+
DateTimeFormatter.ISO_INSTANT.format(Instant.ofEpochMilli(timestamp)), level, message);
8278
}
8379

84-
@Beta
8580
public Map<String, Object> toJson() {
8681
Map<String, Object> map = new HashMap<>();
8782
map.put("timestamp", timestamp);

java/client/test/org/openqa/selenium/logging/LoggingTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import org.junit.Test;
3030

31+
import java.util.Map;
3132
import java.util.logging.Level;
3233

3334
public class LoggingTest {
@@ -54,4 +55,22 @@ public void canCompareLoggingPreferences() {
5455
prefs2.enable(LogType.BROWSER, Level.ALL);
5556
assertThat(prefs2).isEqualTo(prefs1);
5657
}
58+
59+
@Test
60+
public void canRepresentLogEntryAsJson() {
61+
long timestamp = 1572882588202L;
62+
LogEntry entry = new LogEntry(INFO, timestamp, "There is no more cheese");
63+
Map<String, Object> json = entry.toJson();
64+
assertThat(json)
65+
.containsEntry("timestamp", timestamp)
66+
.containsEntry("level", INFO)
67+
.containsEntry("message", "There is no more cheese");
68+
}
69+
70+
@Test
71+
public void canRepresentLogEntryAsString() {
72+
long timestamp = 1572882588202L;
73+
LogEntry entry = new LogEntry(INFO, timestamp, "There is no more cheese");
74+
assertThat(entry.toString()).isEqualTo("[2019-11-04T15:49:48.202Z] [INFO] There is no more cheese");
75+
}
5776
}

0 commit comments

Comments
 (0)