From: Sébastien Lesaint Date: Thu, 3 Dec 2015 17:03:12 +0000 (+0100) Subject: SONAR-7031 fix Logger.warn(String, Throwable) not logging exception X-Git-Tag: 5.3-RC1~46 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=refs%2Fpull%2F679%2Fhead;p=sonarqube.git SONAR-7031 fix Logger.warn(String, Throwable) not logging exception --- 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 @@ -134,6 +134,12 @@ class ConsoleLogger extends BaseLogger { log("WARN ", msg); } + @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 @@ -110,6 +110,11 @@ class LogbackLogger extends BaseLogger { logback.warn(msg); } + @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 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");