Skip to content

Commit

Permalink
feature(logback-classic): add ability to include stack traces when fo…
Browse files Browse the repository at this point in the history
…rwarding logs
  • Loading branch information
fieldju committed May 13, 2022
1 parent 53ca2a3 commit 6140ea2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public LoggingEvent_Instrumentation(String fqcn, Logger logger, Level level, Str

if (applicationLoggingEnabled && isApplicationLoggingForwardingEnabled()) {
// Record and send LogEvent to New Relic
recordNewRelicLogEvent(getFormattedMessage(), timeStamp, level);
recordNewRelicLogEvent(getFormattedMessage(), timeStamp, level, throwable);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.newrelic.agent.bridge.AgentBridge;
import com.newrelic.api.agent.NewRelic;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
Expand All @@ -21,6 +23,7 @@ public class AgentUtil {
public static final int DEFAULT_NUM_OF_LOG_EVENT_ATTRIBUTES = 3;
// Log message attributes
public static final String MESSAGE = "message";
public static final String STACKTRACE = "stacktrace";
public static final String TIMESTAMP = "timestamp";
public static final String LOG_LEVEL = "log.level";
public static final String UNKNOWN = "UNKNOWN";
Expand All @@ -36,6 +39,7 @@ public class AgentUtil {
private static final boolean APP_LOGGING_DEFAULT_ENABLED = true;
private static final boolean APP_LOGGING_METRICS_DEFAULT_ENABLED = true;
private static final boolean APP_LOGGING_FORWARDING_DEFAULT_ENABLED = true;
private static final boolean APP_LOGGING_FORWARD_STACK_TRACES_ENABLED = false;
private static final boolean APP_LOGGING_LOCAL_DECORATING_DEFAULT_ENABLED = false;

/**
Expand All @@ -45,7 +49,7 @@ public class AgentUtil {
* @param timeStampMillis log timestamp
* @param level log level
*/
public static void recordNewRelicLogEvent(String message, long timeStampMillis, Level level) {
public static void recordNewRelicLogEvent(String message, long timeStampMillis, Level level, Throwable t) {
// Bail out and don't create a LogEvent if log message is empty
if (!message.isEmpty()) {
HashMap<String, Object> logEventMap = new HashMap<>(DEFAULT_NUM_OF_LOG_EVENT_ATTRIBUTES);
Expand All @@ -58,6 +62,13 @@ public static void recordNewRelicLogEvent(String message, long timeStampMillis,
logEventMap.put(LOG_LEVEL, level);
}

if (t != null && isApplicationForwardStackTracesEnabled()) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
logEventMap.put(STACKTRACE, sw.toString());
}

AgentBridge.getAgent().getLogSender().recordLogEvent(logEventMap);
}
}
Expand Down Expand Up @@ -134,6 +145,15 @@ public static boolean isApplicationLoggingForwardingEnabled() {
return NewRelic.getAgent().getConfig().getValue("application_logging.forwarding.enabled", APP_LOGGING_FORWARDING_DEFAULT_ENABLED);
}

/**
* Check if the application_logging forward_stack_traces feature is enabled.
*
* @return true if enabled, else false
*/
public static boolean isApplicationForwardStackTracesEnabled() {
return NewRelic.getAgent().getConfig().getValue("application_logging.forward_stack_traces.enabled", APP_LOGGING_FORWARD_STACK_TRACES_ENABLED);
}

/**
* Check if the application_logging local_decorating feature is enabled.
*
Expand Down

0 comments on commit 6140ea2

Please sign in to comment.