Cette page n'est pas encore disponible en français, sa traduction est en cours.
Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.

Before you begin

Ensure log collection is configured. See Java Log Collection for Log4j, Log4j 2, or Logback instructions.

Automatic injection

Starting in version 0.74.0, the Java tracer automatically injects trace correlation identifiers into JSON formatted logs. For earlier versions, enable automatic injection in the Java tracer by adding dd.logs.injection=true as a system property, or through the environment variable DD_LOGS_INJECTION=true. Full configuration details can be found on the Java tracer configuration page.

Notes:

  • Automatic injection of trace correlation is available for Log4j2, Log4j, or SLF4J and Logback.
  • If the attribute.path for your trace ID is not dd.trace_id, ensure that your trace ID reserved attribute settings account for the attribute.path. For more information, see Correlated Logs Not Showing Up in the Trace ID Panel.
Beta: Starting in version 1.18.3, if Agent Remote Configuration is enabled where the service runs, you can set DD_LOGS_INJECTION in the Service Catalog UI.

Manual injection

If you prefer to manually correlate your traces with your logs, use the Java tracer’s API to retrieve correlation identifiers. Use CorrelationIdentifier.getTraceId and CorrelationIdentifier.getSpanId methods to inject identifiers at the beginning of the span being logged, and remove the identifiers when the span is complete.

import org.apache.logging.log4j.ThreadContext;
import datadog.trace.api.CorrelationIdentifier;

// There must be spans started and active before this block.
try {
    ThreadContext.put("dd.trace_id", CorrelationIdentifier.getTraceId());
    ThreadContext.put("dd.span_id", CorrelationIdentifier.getSpanId());

// Log something

} finally {
    ThreadContext.remove("dd.trace_id");
    ThreadContext.remove("dd.span_id");
}
import org.slf4j.MDC;
import datadog.trace.api.CorrelationIdentifier;

// There must be spans started and active before this block.
try {
    MDC.put("dd.trace_id", CorrelationIdentifier.getTraceId());
    MDC.put("dd.span_id", CorrelationIdentifier.getSpanId());

// Log something

} finally {
    MDC.remove("dd.trace_id");
    MDC.remove("dd.span_id");
}
import org.tinylog.ThreadContext;
import datadog.trace.api.CorrelationIdentifier;

// There must be spans started and active before this block.
try {
    ThreadContext.put("dd.trace_id", CorrelationIdentifier.getTraceId());
    ThreadContext.put("dd.span_id", CorrelationIdentifier.getSpanId());

// Log something

} finally {
    ThreadContext.remove("dd.trace_id");
    ThreadContext.remove("dd.span_id");
}

Note: If you are not using a Datadog Log Integration to parse your logs, custom log parsing rules need to ensure that dd.trace_id and dd.span_id are being parsed as strings. For more information, see Correlated Logs Not Showing Up in the Trace ID Panel.

See the Java log collection documentation for more details about specific logger implementation and instructions for logging in JSON format.

Further Reading

PREVIEWING: mervebolat/span-id-preprocessing