From 6140ea2c7acc57abe5a207e46e8a6a22929e2a35 Mon Sep 17 00:00:00 2001 From: Justin Field Date: Thu, 12 May 2022 21:54:46 -0700 Subject: [PATCH] feature(logback-classic): add ability to include stack traces when forwarding logs --- .../spi/LoggingEvent_Instrumentation.java | 2 +- .../logbackclassic12/AgentUtil.java | 22 ++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/instrumentation/logback-classic-1.2/src/main/java/ch/qos/logback/classic/spi/LoggingEvent_Instrumentation.java b/instrumentation/logback-classic-1.2/src/main/java/ch/qos/logback/classic/spi/LoggingEvent_Instrumentation.java index 15859cac00..a25085ab10 100644 --- a/instrumentation/logback-classic-1.2/src/main/java/ch/qos/logback/classic/spi/LoggingEvent_Instrumentation.java +++ b/instrumentation/logback-classic-1.2/src/main/java/ch/qos/logback/classic/spi/LoggingEvent_Instrumentation.java @@ -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); } } diff --git a/instrumentation/logback-classic-1.2/src/main/java/com/nr/agent/instrumentation/logbackclassic12/AgentUtil.java b/instrumentation/logback-classic-1.2/src/main/java/com/nr/agent/instrumentation/logbackclassic12/AgentUtil.java index bdb4f3fd3d..dbf636ab5c 100644 --- a/instrumentation/logback-classic-1.2/src/main/java/com/nr/agent/instrumentation/logbackclassic12/AgentUtil.java +++ b/instrumentation/logback-classic-1.2/src/main/java/com/nr/agent/instrumentation/logbackclassic12/AgentUtil.java @@ -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; @@ -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"; @@ -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; /** @@ -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 logEventMap = new HashMap<>(DEFAULT_NUM_OF_LOG_EVENT_ATTRIBUTES); @@ -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); } } @@ -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. *