Skip to content

Commit

Permalink
Logger: Log context of DruidExceptions. (apache#17316)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
gianm authored Oct 10, 2024
1 parent 074944e commit 1d95ef3
Showing 1 changed file with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

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;
Expand Down Expand Up @@ -103,8 +104,9 @@ public String toString()
}

/**
* 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()
{
Expand Down Expand Up @@ -270,7 +272,14 @@ public boolean isInfoEnabled()

private void logException(BiConsumer<String, Throwable> 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()) {
Expand Down

0 comments on commit 1d95ef3

Please sign in to comment.