8000 Add the possibility to output the logs of the Java tracer in JSON by cecile75 · Pull Request #8083 · DataDog/dd-trace-java · GitHub
[go: up one dir, main page]

Skip to content

Add the possibility to output the logs of the Java tracer in JSON #8083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
initial commit
  • Loading branch information
Cecile Terpin committed Nov 12, 2024
commit 10f5803db4a165ce4924832e3bab0ec3a6b7502b
10000
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public class Agent {

private static final String SIMPLE_LOGGER_SHOW_DATE_TIME_PROPERTY =
"datadog.slf4j.simpleLogger.showDateTime";
private static final String SIMPLE_LOGGER_JSON_ENABLED_PROPERTY =
"datadog.slf4j.simpleLogger.json.enabled";
private static final String SIMPLE_LOGGER_DATE_TIME_FORMAT_PROPERTY =
"datadog.slf4j.simpleLogger.dateTimeFormat";
private static final String SIMPLE_LOGGER_DATE_TIME_FORMAT_DEFAULT =
Expand Down Expand Up @@ -1104,6 +1106,8 @@ private static void configureLogger() {
setSystemPropertyDefault(SIMPLE_LOGGER_SHOW_DATE_TIME_PROPERTY, "true");
setSystemPropertyDefault(
SIMPLE_LOGGER_DATE_TIME_FORMAT_PROPERTY, SIMPLE_LOGGER_DATE_TIME_FORMAT_DEFAULT);
setSystemPropertyDefault(
SIMPLE_LOGGER_JSON_ENABLED_PROPERTY, "false");

String logLevel;
if (isDebugMode()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package datadog.trace.logging.simplelogger;

import datadog.trace.api.Config;
import static datadog.trace.util.Strings.escapeToJson;
import datadog.trace.logging.LogLevel;
import datadog.trace.logging.LoggerHelper;
import org.slf4j.Marker;
Expand Down Expand Up @@ -35,10 +35,11 @@ public void log(LogLevel level, Marker marker, String message, Throwable t) {
if (settings.showDateTime) {
timeMillis = System.currentTimeMillis();
}
if (Config.get().isJsonLogsEnabled()) {
logJson(level, marker, SLCompatFactory.START_TIME, timeMillis, message, t);

if (settings.jsonEnabled) {
logJson(level, marker, SLCompatFactory.START_TIME, timeMillis, message, t);
} else {
log(level, marker, SLCompatFactory.START_TIME, timeMillis, message, t);
log(level, marker, SLCompatFactory.START_TIME, timeMillis, message, t);
}
}

Expand All @@ -56,6 +57,70 @@ void log(
log(level, marker, startTimeMillis, timeMillis, threadName, message, t);
}

void log(
LogLevel level,
Marker marker,
long startTimeMillis,
long timeMillis,
String threadName,
String message,
Throwable t) {
StringBuilder buf = new StringBuilder(32);

if (timeMillis >= 0 && settings.showDateTime) {
settings.dateTimeFormatter.appendFormattedDate(buf, timeMillis, startTimeMillis);
buf.append(' ');
}

if (settings.showThreadName && threadName != null) {
buf.append('[');
buf.append(threadName);
buf.append("] ");
}

if (settings.levelInBrackets) {
buf.append('[');
}

if (settings.warnLevelString != null && level == LogLevel.WARN) {
buf.append(settings.warnLevelString);
} else if (marker != null) {
buf.append(marker.getName());
} else {
buf.append(level.name());
}
if (settings.levelInBrackets) {
buf.append(']');
}
buf.append(' ');

if (!logName.isEmpty()) {
buf.append(logName).append(" - ");
}

buf.append(message);

if (settings.embedException && t != null) {
embedException(buf, t);
}

settings.printStream.println(buf);
if (!settings.embedException && t != null) {
t.printStackTrace(settings.printStream);
}
}

private void embedException(StringBuilder buf, Throwable t) {
buf.append(" [exception:");
buf.append(t.toString());
buf.append(".");
for (StackTraceElement element : t.getStackTrace()) {
buf.append(" at ");
buf.append(element.toString());
}
buf.append("]");
}

void logJson(
LogLevel level,
Marker marker,
Expand All @@ -81,7 +146,7 @@ void logJson(
StringBuilder buf = new StringBuilder(32);

buf.append("{");

if (timeMillis >= 0 && settings.showDateTime) {
embedJsonKey(buf, "time");
settings.dateTimeFormatter.appendFormattedDate(buf, timeMillis, startTimeMillis);
Expand All @@ -102,15 +167,15 @@ void logJson(
}

if (!logName.isEmpty()) {
embedJson(buf, "logName", logName, true);
embedJson(buf, "loggerName", logName, true);
}
embedJson(buf, "message", message, false);

if (t != null) {
buf.append(",");
embedExceptionJson(buf, t);
}

embedJson(buf, "message", message, false);

buf.append("}");

settings.printStream.println(buf);
Expand All @@ -119,94 +184,36 @@ void logJson(
private void embedJson(StringBuilder buf, String key, String value, boolean withComma) {
embedJsonKey(buf, key);
embedJsonValue(buf, value, withComma);

}

private void embedJsonKey(StringBuilder buf, String key) {
buf.append("\"").append(key).append("\":\"");
buf.append("\"").append(escapeToJson(key)).append("\":\"");
}

private void embedJsonValue(StringBuilder buf, String value, boolean withComma) {
buf.append(value).append("\"");

buf.append(escapeToJson(value)).append("\"");
if (withComma) {
buf.append(",");
}
}

void log(
LogLevel level,
Marker marker,
long startTimeMillis,
long timeMillis,
String threadName,
String message,
Throwable t) {
StringBuilder buf = new StringBuilder(32);

if (timeMillis >= 0 && settings.showDateTime) {
settings.dateTimeFormatter.appendFormattedDate(buf, timeMillis, startTimeMillis);
buf.append(' ');
}

if (settings.showThreadName && threadName != null) {
buf.append('[');
buf.append(threadName);
buf.append("] ");
}

if (settings.levelInBrackets) {
buf.append('[');
}

if (settings.warnLevelString != null && level == LogLevel.WARN) {
buf.append(settings.warnLevelString);
} else if (marker != null) {
buf.append(marker.getName());
} else {
buf.append(level.name());
}
if (settings.levelInBrackets) {
buf.append(']');
}
buf.append(' ');

if (!logName.isEmpty()) {
buf.append(logName).append(" - ");
}

buf.append(message);

if (settings.embedException && t != null) {
embedException(buf, t);
}

settings.printStream.println(buf);
if (!settings.embedException && t != null) {
t.printStackTrace(settings.printStream);
}
}

private void embedException(StringBuilder buf, Throwable t) {
buf.append(" [exception:");
buf.append(t.toString());
buf.append(".");
for (StackTraceElement element : t.getStackTrace()) {
buf.append(" at ");
buf.append(element.toString());
}
buf.append("]");
}

private void embedExceptionJson(StringBuilder buf, Throwable t) {
buf.append("\"exception\":{");
embedJson(buf, "message", t.getMessage(), true);
buf.append("\"stackTrace\":[\"");

for (StackTraceElement element : t.getStackTrace()) {
buf.append(element.toString());
buf.append("\",\"");
embedJson(buf, "message",escapeToJson(t.getMessage()), true);
int length = t.getStackTrace().length;
if (length > 0) {
buf.append("\"stackTrace\":[\"");
int count = 0;
for (StackTraceElement element : t.getStackTrace()) {
count += 1;
buf.append(escapeToJson(element.toString()));
if (count != length) {
buf.append("\",\"");
}
}
buf.append("\"]");
}

buf.append("\"]");
buf.append("}");
}
}
F438
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package datadog.trace.logging.simplelogger;

import static java.time.format.DateTimeFormatter.ISO_OFFSET_DATE_TIME;

import datadog.trace.logging.LogLevel;
import datadog.trace.logging.LogReporter;
import datadog.trace.logging.PrintStreamWrapper;
Expand Down Expand Up @@ -33,9 +35,11 @@ public static final class Names {
public static final String SHOW_THREAD_NAME = "showThreadName";
public static final String DATE_TIME_FORMAT = "dateTimeFormat";
public static final String SHOW_DATE_TIME = "showDateTime";
public static final String JSON_ENABLED = "json.enabled";
public static final String DEFAULT_LOG_LEVEL = "defaultLogLevel";
public static final String EMBED_EXCEPTION = "embedException";
public static final String CONFIGURATION_FILE = "configurationFile";

}

public static final class Keys {
Expand All @@ -53,6 +57,7 @@ public static final class Keys {
public static final String SHOW_THREAD_NAME = PREFIX + Names.SHOW_THREAD_NAME;
public static final String DATE_TIME_FORMAT = PREFIX + Names.DATE_TIME_FORMAT;
public static final String SHOW_DATE_TIME = PREFIX + Names.SHOW_DATE_TIME;
public static final String JSON_ENABLED = PREFIX + Names.JSON_ENABLED;
public static final String DEFAULT_LOG_LEVEL = PREFIX + Names.DEFAULT_LOG_LEVEL;
public static final String EMBED_EXCEPTION = PREFIX + Names.EMBED_EXCEPTION;

Expand All @@ -70,6 +75,7 @@ public static final class Defaults {
public static final boolean SHOW_THREAD_NAME = true;
public static final String DATE_TIME_FORMAT = null;
public static final boolean SHOW_DATE_TIME = false;
public static final boolean JSON_ENABLED = false;
public static final String DEFAULT_LOG_LEVEL = "INFO";
public static final boolean EMBED_EXCEPTION = false;

Expand Down Expand Up @@ -271,6 +277,7 @@ static boolean getBoolean(
final boolean showThreadName;
final DTFormatter dateTimeFormatter;
final boolean showDateTime;
final boolean jsonEnabled;
final LogLevel defaultLogLevel;
final boolean embedException;

Expand Down Expand Up @@ -304,6 +311,7 @@ public SLCompatSettings(
getString(
properties, fileProperties, Keys.DATE_TIME_FORMAT, Defaults.DATE_TIME_FORMAT)),
getBoolean(properties, fileProperties, Keys.SHOW_DATE_TIME, Defaults.SHOW_DATE_TIME),
getBoolean(properties, fileProperties, Keys.JSON_ENABLED, Defaults.JSON_ENABLED),
LogLevel.fromString(
getString(
properties, fileProperties, Keys.DEFAULT_LOG_LEVEL, Defaults.DEFAULT_LOG_LEVEL)),
Expand All @@ -321,6 +329,7 @@ public SLCompatSettings(
boolean showThreadName,
DTFormatter dateTimeFormatter,
boolean showDateTime,
boolean jsonEnabled,
LogLevel defaultLogLevel,
boolean embedException) {
this.properties = properties;
Expand All @@ -333,6 +342,7 @@ public SLCompatSettings(
this.showThreadName = showThreadName;
this.dateTimeFormatter = dateTimeFormatter;
this.showDateTime = showDateTime;
this.jsonEnabled = jsonEnabled;
this.defaultLogLevel = defaultLogLevel;
this.embedException = embedException;
}
Expand Down Expand Up @@ -375,6 +385,7 @@ public Map<String, Object> getSettingsDescription() {
settingsDescription.put(Names.SHOW_SHORT_LOG_NAME, showShortLogName);
settingsDescription.put(Names.SHOW_THREAD_NAME, showThreadName);
settingsDescription.put(Names.SHOW_DATE_TIME, showDateTime);
settingsDescription.put(Names.JSON_ENABLED, jsonEnabled);
String dateTimeFormat =
getString(properties, fileProperties, Keys.DATE_TIME_FORMAT, Defaults.DATE_TIME_FORMAT);
settingsDescription.put(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public final class ConfigDefaults {
public static final String DEFAULT_SERVLET_ROOT_CONTEXT_SERVICE_NAME = "root-servlet";
public static final String DEFAULT_AGENT_WRITER_TYPE = "DDAgentWriter";
public static final boolean DEFAULT_STARTUP_LOGS_ENABLED = true;
public static final boolean DEFAULT_JSON_LOGS_ENABLED = false;

static final boolean DEFAULT_WRITER_BAGGAGE_INJECT = true;
static final String DEFAULT_SITE = "datadoghq.com";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public final class GeneralConfig {
public static final String TRIAGE_REPORT_DIR = "triage.report.dir";

public static final String STARTUP_LOGS_ENABLED = "trace.startup.logs";
public static final String JSON_LOGS_ENABLED = "trace.experimental.json.logs";

public static final String DOGSTATSD_START_DELAY = "dogstatsd.start-delay";
public static final String DOGSTATSD_HOST = "dogstatsd.host";
Expand Down
10 changes: 0 additions & 10 deletions internal-api/src/main/java/datadog/trace/api/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@
import static datadog.trace.api.ConfigDefaults.DEFAULT_JMX_FETCH_ENABLED;
import static datadog.trace.api.ConfigDefaults.DEFAULT_JMX_FETCH_MULTIPLE_RUNTIME_SERVICES_ENABLED;
import static datadog.trace.api.ConfigDefaults.DEFAULT_JMX_FETCH_MULTIPLE_RUNTIME_SERVICES_LIMIT;
import static datadog.trace.api.ConfigDefaults.DEFAULT_JSON_LOGS_ENABLED;
import static datadog.trace.api.ConfigDefaults.DEFAULT_LOGS_INJECTION_ENABLED;
import static datadog.trace.api.ConfigDefaults.DEFAULT_PARTIAL_FLUSH_MIN_SPANS;
import static datadog.trace.api.ConfigDefaults.DEFAULT_PERF_METRICS_ENABLED;
Expand Down Expand Up @@ -268,7 +267,6 @@
import static datadog.trace.api.config.GeneralConfig.HEALTH_METRICS_ENABLED;
import static datadog.trace.api.config.GeneralConfig.HEALTH_METRICS_STATSD_HOST;
import static datadog.trace.api.config.GeneralConfig.HEALTH_METRICS_STATSD_PORT;
import static datadog.trace.api.config.GeneralConfig.JSON_LOGS_ENABLED;
import static datadog.trace.api.config.GeneralConfig.LOG_LEVEL;
import static datadog.trace.api.config.GeneralConfig.PERF_METRICS_ENABLED;
import static datadog.trace.api.config.GeneralConfig.PRIMARY_TAG;
Expand Down Expand Up @@ -936,7 +934,6 @@ public static String getHostName() {
private final String triageReportDir;

private final boolean startupLogsEnabled;
private final boolean jsonLogsEnabled;
private final String configFileStatus;

private final IdGenerationStrategy idGenerationStrategy;
Expand Down Expand Up @@ -2109,7 +2106,6 @@ PROFILING_DATADOG_PROFILER_ENABLED, isDatadogProfilerSafeInCurrentEnvironment())

startupLogsEnabled =
configProvider.getBoolean(STARTUP_LOGS_ENABLED, DEFAULT_STARTUP_LOGS_ENABLED);
jsonLogsEnabled = configProvider.getBoolean(JSON_LOGS_ENABLED, DEFAULT_JSON_LOGS_ENABLED);

cwsEnabled = configProvider.getBoolean(CWS_ENABLED, DEFAULT_CWS_ENABLED);
cwsTlsRefresh = configProvider.getInteger(CWS_TLS_REFRESH, DEFAULT_CWS_TLS_REFRESH);
Expand Down Expand Up @@ -3559,10 +3555,6 @@ public boolean isStartupLogsEnabled() {
return startupLogsEnabled;
}

public boolean isJsonLogsEnabled() {
return jsonLogsEnabled;
}

public boolean isCwsEnabled() {
return cwsEnabled;
}
Expand Down Expand Up @@ -4754,8 +4746,6 @@ public String toString() {
+ triageReportDir
+ ", startLogsEnabled="
+ startupLogsEnabled
+ ", jsonLogsEnabled="
+ jsonLogsEnabled
+ ", configFile='"
+ configFileStatus
+ '\''
Expand Down
0