diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2015-12-03 18:03:12 +0100 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2015-12-07 10:44:26 +0100 |
commit | 3e4fbed7522576b9cbf9ee33f62a7d834822e6ff (patch) | |
tree | 60c851c2c2ac7ebd841f8788c48742896ce687a6 /sonar-plugin-api | |
parent | 945ddd5c81c6eb6c1368fe0294bb4ad2f82297a5 (diff) | |
download | sonarqube-3e4fbed7522576b9cbf9ee33f62a7d834822e6ff.tar.gz sonarqube-3e4fbed7522576b9cbf9ee33f62a7d834822e6ff.zip |
SONAR-7031 fix Logger.warn(String, Throwable) not logging exception
Diffstat (limited to 'sonar-plugin-api')
5 files changed, 32 insertions, 4 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/BaseLogger.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/BaseLogger.java index 3c19cc5c673..3bb30155d9b 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/BaseLogger.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/BaseLogger.java @@ -103,7 +103,7 @@ abstract class BaseLogger implements Logger { @Override public void warn(String msg, Throwable throwable) { LogInterceptors.get().log(LoggerLevel.WARN, msg, throwable); - doWarn(msg); + doWarn(msg, throwable); } @Override @@ -180,6 +180,8 @@ abstract class BaseLogger implements Logger { abstract void doWarn(String msg); + abstract void doWarn(String msg, Throwable thrown); + abstract void doWarn(String msg, @Nullable Object arg); abstract void doWarn(String msg, @Nullable Object arg1, @Nullable Object arg2); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/ConsoleLogger.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/ConsoleLogger.java index d58401ed2fc..672e26f3ac9 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/ConsoleLogger.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/ConsoleLogger.java @@ -135,6 +135,12 @@ class ConsoleLogger extends BaseLogger { } @Override + void doWarn(String msg, Throwable thrown) { + doWarn(msg); + thrown.printStackTrace(); + } + + @Override protected void doWarn(String pattern, @Nullable Object arg) { warn(format(pattern, arg)); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/LogbackLogger.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/LogbackLogger.java index 8675a94c5e1..1f2516de343 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/LogbackLogger.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/LogbackLogger.java @@ -111,6 +111,11 @@ class LogbackLogger extends BaseLogger { } @Override + void doWarn(String msg, Throwable thrown) { + logback.warn(msg, thrown); + } + + @Override protected void doWarn(String msg, @Nullable Object arg) { logback.warn(msg, arg); } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/log/ConsoleLoggerTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/log/ConsoleLoggerTest.java index 920f73e4948..7150ffbccab 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/log/ConsoleLoggerTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/log/ConsoleLoggerTest.java @@ -22,6 +22,7 @@ package org.sonar.api.utils.log; import java.io.PrintStream; import org.junit.Rule; import org.junit.Test; +import org.mockito.ArgumentCaptor; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.anyString; @@ -87,20 +88,33 @@ public class ConsoleLoggerTest { } @Test - public void log() { + public void log_info() { underTest.info("message"); underTest.info("message {}", "foo"); underTest.info("message {} {}", "foo", "bar"); underTest.info("message {} {} {}", "foo", "bar", "baz"); verify(stream, times(4)).println(startsWith("INFO ")); + } + + @Test + public void log_warn() { + Throwable throwable = mock(Throwable.class); underTest.warn("message"); underTest.warn("message {}", "foo"); underTest.warn("message {} {}", "foo", "bar"); underTest.warn("message {} {} {}", "foo", "bar", "baz"); - underTest.warn("message", new IllegalArgumentException()); - verify(stream, times(5)).println(startsWith("WARN ")); + underTest.warn("message", throwable); + ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); + verify(stream, times(5)).println(captor.capture()); + for (String msg : captor.getAllValues()) { + assertThat(msg).startsWith("WARN "); + } + verify(throwable).printStackTrace(); + } + @Test + public void log_error() { underTest.error("message"); underTest.error("message {}", "foo"); underTest.error("message {} {}", "foo", "bar"); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/log/LogbackLoggerTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/log/LogbackLoggerTest.java index 9061d429692..c250496324e 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/log/LogbackLoggerTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/log/LogbackLoggerTest.java @@ -56,6 +56,7 @@ public class LogbackLoggerTest { underTest.warn("message {}", "foo"); underTest.warn("message {} {}", "foo", "bar"); underTest.warn("message {} {} {}", "foo", "bar", "baz"); + underTest.warn("message", new NullPointerException("boom!")); underTest.error("message"); underTest.error("message {}", "foo"); |