Refactoring the logging, use slf4j
MODIFY
dd-java-agent/dd-java-agent.gradle
dd-java-agent/dd-java-agent.gradle
@@ -24,6 +24,7 @@ dependencies {
compile group: 'io.opentracing.contrib', name: 'opentracing-agent', version: '0.1.0'
compile group: 'org.reflections', name: 'reflections', version: '0.9.11'
compile group: 'com.google.auto.service', name: 'auto-service', version: '1.0-rc3'
+ compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
compile(group: 'io.opentracing.contrib', name: 'opentracing-web-servlet-filter', version: '0.0.9') {
exclude(group: 'org.eclipse.jetty', module: 'jetty-servlet')

MODIFY
dd-java-agent/src/main/java/com/datadoghq/trace/agent/InstrumentationChecker.java
dd-java-agent/src/main/java/com/datadoghq/trace/agent/InstrumentationChecker.java
@@ -8,6 +8,8 @@ import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* Utility class to check the validity of the classpath concerning the java automated
@@ -15,6 +17,7 @@ import java.util.regex.Pattern;
*/
public class InstrumentationChecker {
+ public static final Logger LOGGER = LoggerFactory.getLogger(InstrumentationChecker.class);
private static final String CONFIG_FILE = "dd-trace-supported-framework.yaml";
private final Map<String, List<Map<String, String>>> rules;
private final Map<String, String> frameworks;
@@ -61,14 +64,20 @@ public class InstrumentationChecker {
Pattern.matches(
check.get("supported_version"), frameworks.get(check.get("artifact")));
if (!matched) {
+ LOGGER.debug(
+ "Library conflict: supported_version={}, actual_version={}",
+ check.get("supported_version"),
+ frameworks.get(check.get("artifact")));
supported = false;
break;
}
supported = true;
+ LOGGER.trace("Instrumentation rule={} is supported", rule);
}
}
if (!supported) {
+ LOGGER.info("Instrumentation rule={} is not supported", rule);
unsupportedRules.add(rule);
}
}

MODIFY
dd-java-agent/src/main/java/com/datadoghq/trace/agent/TraceAnnotationsManager.java
dd-java-agent/src/main/java/com/datadoghq/trace/agent/TraceAnnotationsManager.java
@@ -17,8 +17,6 @@ import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
-import java.util.logging.Level;
-import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javassist.ClassPool;
@@ -33,6 +31,8 @@ import org.reflections.Reflections;
import org.reflections.scanners.MethodAnnotationsScanner;
import org.reflections.util.ConfigurationBuilder;
import org.reflections.util.FilterBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* This manager is loaded at pre-main.
@@ -41,7 +41,8 @@ import org.reflections.util.FilterBuilder;
* all the methods annoted with the @Trace.
*/
public class TraceAnnotationsManager {
- private static Logger log = Logger.getLogger(TraceAnnotationsManager.class.getName());
+
+ private static Logger LOGGER = LoggerFactory.getLogger(TraceAnnotationsManager.class);
private static Retransformer transformer;
@@ -51,7 +52,6 @@ public class TraceAnnotationsManager {
* This method initializes the manager.
*
* @param trans The ByteMan retransformer
- * @throws Exception
*/
public static void initialize(Retransformer trans) throws Exception {
transformer = trans;
@@ -86,7 +86,6 @@ public class TraceAnnotationsManager {
* Uninstall some scripts from a list of patterns. All the rules that contain the pattern will be
* uninstalled
*
- * @param installedScripts
* @param patterns not case sensitive (eg. "mongo", "apache http", "elasticsearch", etc...])
*/
public static void uninstallScripts(List<String> installedScripts, List<String> patterns)
@@ -108,7 +107,7 @@ public class TraceAnnotationsManager {
try (PrintWriter pr = new PrintWriter(sw)) {
transformer.removeScripts(new ArrayList<String>(rulesToRemove), pr);
}
- log.log(Level.INFO, sw.toString());
+ LOGGER.info(sw.toString());
}
}
@@ -121,7 +120,7 @@ public class TraceAnnotationsManager {
public static List<String> loadRules(ClassLoader classLoader) {
List<String> scripts = new ArrayList<>();
if (transformer == null) {
- log.severe("Attempt to load OpenTracing agent rules before transformer initialized");
+ LOGGER.warn("Attempt to load OpenTracing agent rules before transformer initialized");
return scripts;
}
@@ -139,31 +138,23 @@ public class TraceAnnotationsManager {
try {
transformer.installScript(scripts, scriptNames, writer);
} catch (Exception e) {
- log.log(Level.SEVERE, "Failed to install scripts", e);
- }
+ LOGGER.warn("Failed to install scripts", e);
}
- if (log.isLoggable(Level.FINEST)) {
- log.finest(sw.toString());
}
+ LOGGER.trace(sw.toString());
} catch (IOException | URISyntaxException e) {
- log.log(Level.SEVERE, "Failed to load OpenTracing agent rules", e);
+ LOGGER.warn("Failed to load OpenTracing agent rules", e);
}
- if (log.isLoggable(Level.FINE)) {
- log.fine("OpenTracing Agent rules loaded");
- }
+ LOGGER.debug("OpenTracing Agent rules loaded");
return scripts;
}
- /**
- * Find all the methods annoted with @Trace and inject rules
- *
- * @param scannedPackages
- */
+ /** Find all the methods annoted with @Trace and inject rules */
public static void loadAnnotationsRules(String... scannedPackages) {
- log.info(
- "Looking for annotations over the following packages: " + Arrays.asList(scannedPackages));
+ LOGGER.info(
+ "Looking for annotations over the following packages: {}", Arrays.asList(scannedPackages));
long cur = System.currentTimeMillis();
Reflections reflections =
@@ -205,10 +196,8 @@ public class TraceAnnotationsManager {
generatedScripts.append(script).append("\n");
} catch (Exception e) {
- log.log(
- Level.SEVERE,
- "Could not create rule for method " + method + ". Proceed to next annoted method.",
- e);
+ LOGGER.warn(
+ "Could not create rule for method " + method + ". Proceed to next annoted method.", e);
}
}
try {
@@ -217,12 +206,12 @@ public class TraceAnnotationsManager {
transformer.installScript(
Arrays.asList(generatedScripts.toString()), Arrays.asList("@Trace annotations"), pr);
}
- log.log(Level.FINE, sw.toString());
+ LOGGER.debug(sw.toString());
} catch (Exception e) {
- log.log(Level.SEVERE, "Could not install annotation scripts.", e);
+ LOGGER.warn("Could not install annotation scripts.", e);
}
- log.info(
+ LOGGER.info(
"Finished annotation scanning in "
+ (System.currentTimeMillis() - cur)
+ " ms. You can accelerate this process by refining the packages you want to scan with `scannedPackages` in the dd-trace.yaml configuration file.");
@@ -252,12 +241,11 @@ public class TraceAnnotationsManager {
private static void loadRules(URI uri, final List<String> scriptNames, final List<String> scripts)
throws IOException {
- if (log.isLoggable(Level.FINE)) {
- log.fine("Load rules from URI = " + uri);
- }
+ LOGGER.debug("Load rules from URI uri={} ", uri);
StringBuilder str = new StringBuilder();
try (InputStream is = uri.toURL().openStream()) {
+
byte[] b = new byte[10240];
int len;
while ((len = is.read(b)) != -1) {
@@ -285,8 +273,7 @@ public class TraceAnnotationsManager {
return BUILD_SPAN + trace.operationName() + CLOSE_PARENTHESIS;
}
} catch (Exception e) {
- log.log(
- Level.WARNING,
+ LOGGER.warn(
"Error when building injection rule on method "
+ javassistMethod
+ ". Fallback on default value.",

MODIFY
dd-java-agent/src/main/java/com/datadoghq/trace/agent/integration/DDAgentTracingHelper.java
dd-java-agent/src/main/java/com/datadoghq/trace/agent/integration/DDAgentTracingHelper.java
@@ -3,9 +3,9 @@ package com.datadoghq.trace.agent.integration;
import io.opentracing.NoopTracerFactory;
import io.opentracing.Tracer;
import io.opentracing.contrib.agent.OpenTracingHelper;
-import java.util.logging.Level;
-import java.util.logging.Logger;
import org.jboss.byteman.rule.Rule;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* This class provides helpful stuff in order to easy patch object using Byteman rules
@@ -14,8 +14,7 @@ import org.jboss.byteman.rule.Rule;
*/
public abstract class DDAgentTracingHelper<T> extends OpenTracingHelper {
- private static final Logger LOGGER =
- Logger.getLogger(DDAgentTracingHelper.class.getCanonicalName());
+ private static final Logger LOGGER = LoggerFactory.getLogger(DDAgentTracingHelper.class);
/**
* The current instance of the tracer. If something goes wrong during the resolution, we provides
@@ -31,8 +30,8 @@ public abstract class DDAgentTracingHelper<T> extends OpenTracingHelper {
tracerResolved = tracerResolved == null ? NoopTracerFactory.create() : tracerResolved;
} catch (Exception e) {
tracerResolved = NoopTracerFactory.create();
- warning("Failed to retrieve the tracer, using a NoopTracer instead: " + e.getMessage());
- logStackTrace(e.getMessage(), e);
+ LOGGER.warn("Failed to retrieve the tracer, using a NoopTracer instead: {}", e.getMessage());
+ LOGGER.warn(e.getMessage(), e);
}
tracer = tracerResolved;
}
@@ -50,20 +49,20 @@ public abstract class DDAgentTracingHelper<T> extends OpenTracingHelper {
public T patch(T args) {
if (args == null) {
- info("Skipping " + rule.getName() + "' rule because the input arg is null");
- return args;
+ LOGGER.debug("Skipping rule={} because the input arg is null", rule.getName());
+ return null;
}
String className = args.getClass().getName();
- info("Try to patch " + className);
+ LOGGER.debug("Try to patch class={}", className);
T patched;
try {
patched = doPatch(args);
- info(className + " patched");
+ LOGGER.debug("class={} patched", className);
} catch (Throwable e) {
- warning("Failed to patch" + className + ", reason: " + e.getMessage());
- logStackTrace(e.getMessage(), e);
+ LOGGER.warn("Failed to patch class={}, reason: {}", className, e.getMessage());
+ LOGGER.warn(e.getMessage(), e);
patched = args;
}
return patched;
@@ -77,36 +76,4 @@ public abstract class DDAgentTracingHelper<T> extends OpenTracingHelper {
* @throws Exception The exceptions are managed directly to the patch method
*/
protected abstract T doPatch(T obj) throws Exception;
-
- /**
- * Simple wrapper to emit a warning
- *
- * @param message the message to log as a warning
- */
- protected void warning(String message) {
- log(Level.WARNING, message);
- }
-
- /**
- * Simple wrapper to emit an info
- *
- * @param message the message to log as an info
- */
- protected void info(String message) {
- log(Level.INFO, message);
- }
-
- /**
- * Simple wrapper to emit the corresponding stacktrace. To not warn the user, we log the stack as
- * a debug info. By default, the stack traces are noit shown in the log.
- *
- * @param message the stacktrace to log as a debug
- */
- protected void logStackTrace(String message, Throwable th) {
- LOGGER.log(Level.FINE, message, th);
- }
-
- private void log(Level level, String message) {
- LOGGER.log(level, String.format("%s - %s", getClass().getSimpleName(), message));
- }
}

MODIFY
dd-java-agent/src/test/resources/logback.xml
dd-java-agent/src/test/resources/logback.xml
</layout>
</appender>
- <root level="INFO">
+ <root level="WARN">
<appender-ref ref="console"/>
</root>
+ <logger name="com.datadoghq" level="debug"/>
+
</configuration>

Refactoring the logging, use slf4j