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.
Automatic injection
Given the many different ways to implement logging in PHP, with some completely circumventing PHP’s built-in error-logging API, the Datadog PHP tracing library cannot reliably inject trace and span IDs into logs automatically.
See the section below to learn how to connect your PHP Logs and traces manually.
Manual injection
Note that the function
\DDTrace\current_context()
has been introduced in version
0.61.0.
To connect your logs and traces together, your logs must contain the dd.trace_id
and dd.span_id
attributes that respectively contain your trace ID and your span ID.
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 and remapped thanks to the Trace Remapper. More information can be found in Correlated Logs Not Showing Up in the Trace ID Panel.
For instance, you would append those two attributes to your logs with:
<?php
$context = \DDTrace\current_context();
$append = sprintf(
' [dd.trace_id=%s dd.span_id=%s]',
$context['trace_id'],
$context['span_id']
);
my_error_logger('Error message.' . $append);
?>
If the logger implements the monolog/monolog library, use Logger::pushProcessor()
to automatically append the identifiers to all log messages. For monolog v1:
<?php
$logger->pushProcessor(function ($record) {
$context = \DDTrace\current_context();
$record['message'] .= sprintf(
' [dd.trace_id=%s dd.span_id=%s]',
$context['trace_id'],
$context['span_id']
);
return $record;
});
?>
For monolog v2:
<?php
$logger->pushProcessor(function ($record) {
$context = \DDTrace\current_context();
return $record->with(message: $record['message'] . sprintf(
' [dd.trace_id=%s dd.span_id=%s]',
$context['trace_id'],
$context['span_id']
));
});
?>
For monolog v3:
<?php
$logger->pushProcessor(function ($record) {
$context = \DDTrace\current_context();
$record->extra['dd'] = [
'trace_id' => $context['trace_id'],
'span_id' => $context['span_id'],
];
return $record;
});
?>
If your application uses json logs format instead of appending trace_id and span_id to the log message you can add first-level key “dd” containing these ids:
<?php
$context = \DDTrace\current_context();
$logger->pushProcessor(function ($record) use ($context) {
$record['dd'] = [
'trace_id' => $context['trace_id'],
'span_id' => $context['span_id'],
];
return $record;
});
?>
Further Reading
Documentation, liens et articles supplémentaires utiles: