From 12c373fad66da9ccb9cfb2ce7e3c5d2de4f60fe2 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Thu, 15 Sep 2016 11:56:46 +0200 Subject: [PATCH] SQSCANNER-30 Add timestamp to logs in debug/verbose --- .../org/sonarsource/scanner/cli/Logs.java | 28 +++++++++++++++---- .../org/sonarsource/scanner/cli/LogsTest.java | 11 ++++---- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/sonarsource/scanner/cli/Logs.java b/src/main/java/org/sonarsource/scanner/cli/Logs.java index e4fc31b..c86decf 100644 --- a/src/main/java/org/sonarsource/scanner/cli/Logs.java +++ b/src/main/java/org/sonarsource/scanner/cli/Logs.java @@ -20,8 +20,11 @@ package org.sonarsource.scanner.cli; import java.io.PrintStream; +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; public class Logs { + private DateTimeFormatter timeFormatter; private boolean debugEnabled = false; private boolean displayStackTrace = false; private PrintStream stdOut; @@ -30,6 +33,7 @@ public class Logs { public Logs(PrintStream stdOut, PrintStream stdErr) { this.stdErr = stdErr; this.stdOut = stdOut; + this.timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss.SSS"); } public void setDebugEnabled(boolean debugEnabled) { @@ -39,33 +43,45 @@ public class Logs { public void setDisplayStackTrace(boolean displayStackTrace) { this.displayStackTrace = displayStackTrace; } - + public boolean isDebugEnabled() { return debugEnabled; } public void debug(String message) { if (isDebugEnabled()) { - stdOut.println("DEBUG: " + message); + LocalTime currentTime = LocalTime.now(); + String timestamp = currentTime.format(timeFormatter); + stdOut.println(timestamp + " DEBUG: " + message); } } public void info(String message) { - stdOut.println("INFO: " + message); + print(stdOut, "INFO: " + message); } public void warn(String message) { - stdOut.println("WARN: " + message); + print(stdErr, "WARN: " + message); } public void error(String message) { - stdErr.println("ERROR: " + message); + print(stdErr, "ERROR: " + message); } public void error(String message, Throwable t) { - stdErr.println("ERROR: " + message); + print(stdErr, "ERROR: " + message); if (t != null && displayStackTrace) { t.printStackTrace(stdErr); } } + + private void print(PrintStream stream, String msg) { + if (debugEnabled) { + LocalTime currentTime = LocalTime.now(); + String timestamp = currentTime.format(timeFormatter); + stream.println(timestamp + " " + msg); + } else { + stream.println(msg); + } + } } diff --git a/src/test/java/org/sonarsource/scanner/cli/LogsTest.java b/src/test/java/org/sonarsource/scanner/cli/LogsTest.java index c3b5e32..cc8ab38 100644 --- a/src/test/java/org/sonarsource/scanner/cli/LogsTest.java +++ b/src/test/java/org/sonarsource/scanner/cli/LogsTest.java @@ -21,6 +21,7 @@ package org.sonarsource.scanner.cli; import org.junit.Before; import org.junit.Test; +import org.mockito.Matchers; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.sonarsource.scanner.cli.Logs; @@ -50,19 +51,19 @@ public class LogsTest { verify(stdOut).println("INFO: info"); verifyNoMoreInteractions(stdOut, stdErr); } - + @Test public void testError() { Exception e = new NullPointerException("exception"); logs.setDisplayStackTrace(false); logs.error("error1"); verify(stdErr).println("ERROR: error1"); - + logs.error("error2", e); verify(stdErr).println("ERROR: error2"); - + verifyNoMoreInteractions(stdOut, stdErr); - + logs.setDisplayStackTrace(true); logs.error("error3", e); verify(stdErr).println("ERROR: error3"); @@ -74,7 +75,7 @@ public class LogsTest { logs.setDebugEnabled(true); logs.debug("debug"); - verify(stdOut).println("DEBUG: debug"); + verify(stdOut).println(Matchers.matches("\\d\\d:\\d\\d:\\d\\d.\\d\\d\\d DEBUG: debug$")); logs.setDebugEnabled(false); logs.debug("debug"); -- 2.39.5