From 1dae583a5cca52b34f6a11ed6cf515998ca06d4b Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Wed, 16 Sep 2015 16:35:44 +0200 Subject: [PATCH] Improve logging of PostJob and Sensor objects --- .../sonar/batch/phases/PhasesTimeProfiler.java | 4 +++- .../org/sonar/batch/phases/PostJobsExecutor.java | 6 ++++-- .../java/org/sonar/batch/util/BatchUtils.java | 13 ++++++++++++- .../org/sonar/batch/util/BatchUtilsTest.java | 16 ++++++++++++++++ 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/PhasesTimeProfiler.java b/sonar-batch/src/main/java/org/sonar/batch/phases/PhasesTimeProfiler.java index 87f2f9e2eea..15aaf1df744 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/phases/PhasesTimeProfiler.java +++ b/sonar-batch/src/main/java/org/sonar/batch/phases/PhasesTimeProfiler.java @@ -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(); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/PostJobsExecutor.java b/sonar-batch/src/main/java/org/sonar/batch/phases/PostJobsExecutor.java index 423cf63e49e..c211d5e0e0a 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/phases/PostJobsExecutor.java +++ b/sonar-batch/src/main/java/org/sonar/batch/phases/PostJobsExecutor.java @@ -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 postJobs) { + private static void logPostJobs(Collection postJobs) { if (LOG.isDebugEnabled()) { LOG.debug("Post-jobs : {}", StringUtils.join(postJobs, " -> ")); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/util/BatchUtils.java b/sonar-batch/src/main/java/org/sonar/batch/util/BatchUtils.java index 3051ba40067..42d53af38cc 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/util/BatchUtils.java +++ b/sonar-batch/src/main/java/org/sonar/batch/util/BatchUtils.java @@ -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(); + } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/util/BatchUtilsTest.java b/sonar-batch/src/test/java/org/sonar/batch/util/BatchUtilsTest.java index 7926172f078..dfe663d016c 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/util/BatchUtilsTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/util/BatchUtilsTest.java @@ -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"); + } } -- 2.39.5