diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2016-09-15 11:56:46 +0200 |
---|---|---|
committer | Duarte Meneses <duarte.meneses@sonarsource.com> | 2016-09-15 11:58:09 +0200 |
commit | 12c373fad66da9ccb9cfb2ce7e3c5d2de4f60fe2 (patch) | |
tree | b7c29efe7b57efd74939950f2587092d2b0f6086 /src/main/java/org/sonarsource/scanner | |
parent | 7a0e70b84e3d3b97dca3db1381d6c64905b9ea6b (diff) | |
download | sonar-scanner-cli-12c373fad66da9ccb9cfb2ce7e3c5d2de4f60fe2.tar.gz sonar-scanner-cli-12c373fad66da9ccb9cfb2ce7e3c5d2de4f60fe2.zip |
SQSCANNER-30 Add timestamp to logs in debug/verbose
Diffstat (limited to 'src/main/java/org/sonarsource/scanner')
-rw-r--r-- | src/main/java/org/sonarsource/scanner/cli/Logs.java | 28 |
1 files changed, 22 insertions, 6 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); + } + } } |