From 1d95ef34f0ed9f12c96a9fdf5a04a5fe114e0107 Mon Sep 17 00:00:00 2001 From: Gian Merlino Date: Thu, 10 Oct 2024 01:44:50 -0700 Subject: [PATCH] Logger: Log context of DruidExceptions. (#17316) * Logger: Log context of DruidExceptions. There is often interesting and unique information available in the "context" of a DruidException. This information is additive to both the message and the cause, and was missed when we log. This patch adds the DruidException context to log messages whenever stack traces are enabled. * Only log nonempty contexts. --- .../druid/java/util/common/logger/Logger.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/processing/src/main/java/org/apache/druid/java/util/common/logger/Logger.java b/processing/src/main/java/org/apache/druid/java/util/common/logger/Logger.java index 59f2eb839b9..b1f21c3d9e1 100644 --- a/processing/src/main/java/org/apache/druid/java/util/common/logger/Logger.java +++ b/processing/src/main/java/org/apache/druid/java/util/common/logger/Logger.java @@ -21,6 +21,7 @@ package org.apache.druid.java.util.common.logger; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; +import org.apache.druid.error.DruidException; import org.apache.druid.java.util.common.StringUtils; import org.apache.druid.timeline.DataSegment; import org.apache.druid.timeline.SegmentId; @@ -103,8 +104,9 @@ public class Logger } /** - * Returns a copy of this Logger that does not log exception stack traces, unless the log level is DEBUG or lower. - * Useful for writing code like: {@code log.noStackTrace().warn(e, "Something happened.");} + * Returns a copy of this Logger that does not log exception stack traces or {@link DruidException#getContext()}, + * unless the log level is DEBUG or lower. Useful for writing code like: + * {@code log.noStackTrace().warn(e, "Something happened.");} */ public Logger noStackTrace() { @@ -270,7 +272,14 @@ public class Logger private void logException(BiConsumer fn, Throwable t, String message) { - if (stackTraces || log.isDebugEnabled()) { + final boolean logStackTrace = stackTraces || log.isDebugEnabled(); + + if (logStackTrace) { + // If logging stack traces, *also* log extra context information about DruidExceptions. + if (t instanceof DruidException && !((DruidException) t).getContext().isEmpty()) { + message = (message == null ? "" : message + "\nDruidException context: " + ((DruidException) t).getContext()); + } + fn.accept(message, t); } else { if (message.isEmpty()) {