]> source.dussan.org Git - sonarqube.git/commitdiff
Improve logging of PostJob and Sensor objects
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Wed, 16 Sep 2015 14:35:44 +0000 (16:35 +0200)
committerDuarte Meneses <duarte.meneses@sonarsource.com>
Wed, 30 Sep 2015 14:27:12 +0000 (16:27 +0200)
sonar-batch/src/main/java/org/sonar/batch/phases/PhasesTimeProfiler.java
sonar-batch/src/main/java/org/sonar/batch/phases/PostJobsExecutor.java
sonar-batch/src/main/java/org/sonar/batch/util/BatchUtils.java
sonar-batch/src/test/java/org/sonar/batch/util/BatchUtilsTest.java

index 87f2f9e2eea938ff846ad945498156bf1e2ea0c4..15aaf1df744a49c7a7dbed2fca239b7b405611f9 100644 (file)
@@ -19,6 +19,8 @@
  */
 package org.sonar.batch.phases;
 
+
+import org.sonar.batch.util.BatchUtils;
 import org.apache.commons.lang.StringUtils;
 import org.sonar.api.batch.events.SensorExecutionHandler;
 import org.sonar.api.batch.events.SensorsPhaseHandler;
@@ -42,7 +44,7 @@ public class PhasesTimeProfiler implements SensorExecutionHandler, SensorsPhaseH
   @Override
   public void onSensorExecution(SensorExecutionEvent event) {
     if (event.isStart()) {
-      profiler.startInfo("Sensor " + event.getSensor());
+      profiler.startInfo("Sensor " + BatchUtils.describe(event.getSensor()));
     } else {
       profiler.stopInfo();
     }
index 423cf63e49ec9bcf2986f272859a9eacceaa914f..c211d5e0e0a627546ba9626fa2b59300e7b508cd 100644 (file)
@@ -19,6 +19,8 @@
  */
 package org.sonar.batch.phases;
 
+import org.sonar.batch.util.BatchUtils;
+
 import com.google.common.collect.Lists;
 import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
@@ -58,14 +60,14 @@ public class PostJobsExecutor {
     logPostJobs(postJobs);
 
     for (PostJob postJob : postJobs) {
-      LOG.info("Executing post-job {}", postJob.toString());
+      LOG.info("Executing post-job {}", BatchUtils.describe(postJob));
       eventBus.fireEvent(new PostJobExecutionEvent(postJob, true));
       postJob.executeOn(project, context);
       eventBus.fireEvent(new PostJobExecutionEvent(postJob, false));
     }
   }
 
-  private void logPostJobs(Collection<PostJob> postJobs) {
+  private static void logPostJobs(Collection<PostJob> postJobs) {
     if (LOG.isDebugEnabled()) {
       LOG.debug("Post-jobs : {}", StringUtils.join(postJobs, " -> "));
     }
index 3051ba4006700225040a6b17166bef5454d8d16d..42d53af38cc6d1aa9fe8f8f1d6c0209718c525de 100644 (file)
@@ -22,7 +22,6 @@ package org.sonar.batch.util;
 import com.google.common.base.Strings;
 import java.io.UnsupportedEncodingException;
 import java.net.URLEncoder;
-
 import javax.annotation.Nullable;
 import org.apache.commons.lang.StringUtils;
 
@@ -48,4 +47,16 @@ public class BatchUtils {
       throw new IllegalStateException("Encoding not supported", e);
     }
   }
+
+  public static String describe(Object o) {
+    try {
+      if (o.getClass().getMethod("toString").getDeclaringClass() != Object.class) {
+        return o.toString();
+      }
+    } catch (Exception e) {
+      // fallback
+    }
+
+    return o.getClass().getName();
+  }
 }
index 7926172f078d19cfd780d8428d43eb2a7d3c6637..dfe663d016c3def7049cacefaffabc5d42de817d 100644 (file)
@@ -32,4 +32,20 @@ public class BatchUtilsTest {
     assertThat(BatchUtils.encodeForUrl("foo")).isEqualTo("foo");
     assertThat(BatchUtils.encodeForUrl("foo&bar")).isEqualTo("foo%26bar");
   }
+
+  @Test
+  
+  public void testDescribe() {
+    Object withToString = new Object() {
+      @Override
+      public String toString() {
+        return "desc";
+      }
+    };
+
+    Object withoutToString = new Object();
+
+    assertThat(BatchUtils.describe(withToString)).isEqualTo(("desc"));
+    assertThat(BatchUtils.describe(withoutToString)).isEqualTo("java.lang.Object");
+  }
 }