aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2016-09-15 11:56:46 +0200
committerDuarte Meneses <duarte.meneses@sonarsource.com>2016-09-15 11:58:09 +0200
commit12c373fad66da9ccb9cfb2ce7e3c5d2de4f60fe2 (patch)
treeb7c29efe7b57efd74939950f2587092d2b0f6086 /src
parent7a0e70b84e3d3b97dca3db1381d6c64905b9ea6b (diff)
downloadsonar-scanner-cli-12c373fad66da9ccb9cfb2ce7e3c5d2de4f60fe2.tar.gz
sonar-scanner-cli-12c373fad66da9ccb9cfb2ce7e3c5d2de4f60fe2.zip
SQSCANNER-30 Add timestamp to logs in debug/verbose
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/sonarsource/scanner/cli/Logs.java28
-rw-r--r--src/test/java/org/sonarsource/scanner/cli/LogsTest.java11
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");