This is an automated email from the ASF dual-hosted git repository.

cschneider pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-distribution-journal.git


The following commit(s) were added to refs/heads/master by this push:
     new 107488e  SLING-10612 - Fix time in distribution log
107488e is described below

commit 107488e2933c2e146a7290af5da6217b754144c6
Author: Christian Schneider <[email protected]>
AuthorDate: Wed Jul 14 19:10:45 2021 +0200

    SLING-10612 - Fix time in distribution log
---
 .../journal/shared/DefaultDistributionLog.java     | 12 +++---
 .../journal/shared/DefaultDistributionLogTest.java | 50 ++++++++++++++++++++++
 2 files changed, 55 insertions(+), 7 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/distribution/journal/shared/DefaultDistributionLog.java
 
b/src/main/java/org/apache/sling/distribution/journal/shared/DefaultDistributionLog.java
index 80c3747..105bcf0 100644
--- 
a/src/main/java/org/apache/sling/distribution/journal/shared/DefaultDistributionLog.java
+++ 
b/src/main/java/org/apache/sling/distribution/journal/shared/DefaultDistributionLog.java
@@ -22,7 +22,7 @@ package org.apache.sling.distribution.journal.shared;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
-import java.util.Calendar;
+import java.util.Date;
 import java.util.LinkedList;
 import java.util.List;
 
@@ -43,7 +43,6 @@ public class DefaultDistributionLog implements 
DistributionLog {
     private final LogLevel logLevel;
     
     private final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd 
HH:mm:ss:SSS");
-    private final Calendar cal = Calendar.getInstance();
 
     public DefaultDistributionLog(String name, Class<?> clazz, LogLevel 
logLevel) {
         this.name = name;
@@ -74,11 +73,10 @@ public class DefaultDistributionLog implements 
DistributionLog {
         if (level.cardinal < logLevel.cardinal) {
             return;
         }
-        String log = dateFormat.format(cal.getTime()) +
-                " - " +
-                level.name() +
-                " - " +
-                message;
+        String log = String.format("%s - %s - %s", 
+                dateFormat.format(new Date().getTime()),
+                level.name(),
+                message);
         addLine(log);
     }
 
diff --git 
a/src/test/java/org/apache/sling/distribution/journal/shared/DefaultDistributionLogTest.java
 
b/src/test/java/org/apache/sling/distribution/journal/shared/DefaultDistributionLogTest.java
new file mode 100644
index 0000000..5b48a64
--- /dev/null
+++ 
b/src/test/java/org/apache/sling/distribution/journal/shared/DefaultDistributionLogTest.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.distribution.journal.shared;
+
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.Matchers.equalTo;
+import static org.junit.Assert.assertThat;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+import 
org.apache.sling.distribution.journal.impl.publisher.DistributionPublisher;
+import 
org.apache.sling.distribution.journal.shared.DefaultDistributionLog.LogLevel;
+import org.junit.Test;
+
+public class DefaultDistributionLogTest {
+
+    @Test
+    public void testTime() throws InterruptedException {
+        DefaultDistributionLog log = new DefaultDistributionLog("name", 
DistributionPublisher.class, LogLevel.INFO);
+        log.info("Test1");
+        Thread.sleep(2);
+        log.info("Test2");
+        List<String> lines = 
log.getLines().stream().map(this::getTime).collect(Collectors.toList());
+        String time1 = lines.get(0);
+        String time2 = lines.get(1);
+        System.out.println(time1);
+        assertThat("Times of log entries must be different " + time1 + " " + 
time2, time1, not(equalTo(time2)));
+    }
+    
+    String getTime(String logLine) {
+        return logLine.split(" ")[1];
+    }
+}

Reply via email to