]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7540 Throwable attached to logs is lost on scanner side when using LogOutput API 893/head
authorJulien HENRY <julien.henry@sonarsource.com>
Fri, 8 Apr 2016 16:10:28 +0000 (18:10 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Mon, 11 Apr 2016 08:26:57 +0000 (10:26 +0200)
plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/MeasureSensor.java
sonar-scanner-engine/src/main/java/org/sonar/batch/bootstrapper/LogCallbackAppender.java
sonar-scanner-engine/src/test/java/org/sonar/batch/bootstrapper/LogCallbackAppenderTest.java
sonar-scanner-engine/src/test/java/org/sonar/batch/mediumtest/log/LogListenerTest.java

index 9562f300fb1e773a48dd464ac13c814795415eb1..d03537c809493e8bf39a376fe8212fd05be8c4f5 100644 (file)
@@ -77,6 +77,7 @@ public class MeasureSensor implements Sensor {
       String value = line.substring(metricKey.length() + 1);
       saveMeasure(context, inputFile, metricKey, value);
     } catch (Exception e) {
+      LOG.error("Error processing line " + lineNumber + " of file " + measureFile.getAbsolutePath(), e);
       throw new IllegalStateException("Error processing line " + lineNumber + " of file " + measureFile.getAbsolutePath(), e);
     }
   }
index 183cd3787114bff94e5ac0a97fa10d7f417f2c90..f401b227d0d13c1ea99277bd7d871555d1c32c6a 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.batch.bootstrapper;
 
 import ch.qos.logback.classic.Level;
+import ch.qos.logback.classic.pattern.ExtendedThrowableProxyConverter;
 import ch.qos.logback.classic.spi.ILoggingEvent;
 import ch.qos.logback.core.UnsynchronizedAppenderBase;
 
@@ -36,7 +37,14 @@ public class LogCallbackAppender extends UnsynchronizedAppenderBase<ILoggingEven
 
   @Override
   protected void append(ILoggingEvent event) {
-    target.log(event.getFormattedMessage(), translate(event.getLevel()));
+    if (event.getThrowableProxy() == null) {
+      target.log(event.getFormattedMessage(), translate(event.getLevel()));
+    } else {
+      ExtendedThrowableProxyConverter throwableConverter = new ExtendedThrowableProxyConverter();
+      throwableConverter.start();
+      target.log(event.getFormattedMessage() + "\n" + throwableConverter.convert(event), translate(event.getLevel()));
+      throwableConverter.stop();
+    }
   }
 
   private static LogOutput.Level translate(Level level) {
index 6b225306fe0973fd4ababa899a3d638582ce1917..14dc014a2aefde5ef9efd6d70a875508ebad6283 100644 (file)
@@ -63,6 +63,7 @@ public class LogCallbackAppenderTest {
 
     verify(event).getFormattedMessage();
     verify(event).getLevel();
+    verify(event).getThrowableProxy();
     verify(listener).log(msg, translatedLevel);
     verifyNoMoreInteractions(event, listener);
   }
index f360dca56ac8103c26bd196c5ba3df277c25fd71..4e4a4857ede3b917dcdb1af7bf7d0d93c7db8b3c 100644 (file)
@@ -20,7 +20,6 @@
 package org.sonar.batch.mediumtest.log;
 
 import com.google.common.collect.ImmutableMap;
-
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
@@ -30,7 +29,6 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
-
 import org.apache.commons.io.FileUtils;
 import org.junit.AfterClass;
 import org.junit.Before;
@@ -40,9 +38,12 @@ import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.junit.rules.TemporaryFolder;
 import org.sonar.batch.bootstrapper.LogOutput;
+import org.sonar.batch.bootstrapper.LogOutput.Level;
 import org.sonar.batch.mediumtest.BatchMediumTester;
 import org.sonar.xoo.XooPlugin;
+
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.fail;
 
 public class LogListenerTest {
   @Rule
@@ -196,6 +197,42 @@ public class LogListenerTest {
     }
   }
 
+  // SONAR-7540
+  @Test
+  public void testStackTrace() throws IOException {
+    File srcDir = new File(baseDir, "src");
+    srcDir.mkdir();
+
+    File xooFile = new File(srcDir, "sample.xoo");
+    FileUtils.write(xooFile, "Sample xoo\ncontent");
+    File xooFileMeasure = new File(srcDir, "sample.xoo.measures");
+    FileUtils.write(xooFileMeasure, "foo:bar");
+
+    try {
+      tester.newTask()
+        .properties(builder
+          .put("sonar.sources", "src")
+          .build())
+        .start();
+      fail("Expected exception");
+    } catch (Exception e) {
+      assertThat(e.getMessage()).contains("Error processing line 1");
+    }
+    tester.stop();
+
+    assertNoStdOutput();
+
+    synchronized (logOutput) {
+      for (LogEvent e : logOutput) {
+        if (e.level == Level.ERROR) {
+          assertThat(e.msg).contains("Error processing line 1 of file", "src/sample.xoo.measures", "java.lang.IllegalStateException: Unknow metric with key: foo",
+            "at org.sonar.xoo.lang.MeasureSensor.saveMeasure");
+
+        }
+      }
+    }
+  }
+
   private class SimpleLogListener implements LogOutput {
     @Override
     public void log(String msg, Level level) {