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.
This commit is contained in:
Gian Merlino 2024-10-10 01:44:50 -07:00 committed by GitHub
parent 074944e02c
commit 1d95ef34f0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 12 additions and 3 deletions

View File

@ -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<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()) {