PHP: invalid Monolog snippet
gnumoksha opened this issue · comments
Documentation Page
- file other_telemetry/connect_logs_and_traces/php.md
- url https://docs.datadoghq.com/tracing/other_telemetry/connect_logs_and_traces/php/
Issue Description
The following snippet does not work because Monolog does not allow the processor to set the message:
<?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;
});
?>
That throws a LogicException
with the message Unsupported operation: setting message
.
Hi! Thanks for raising this Tobias. I'll look into what it should say and fix it.
This changed in monolog v2 - the code snippet here is compatible with v1.
For v2 use:
<?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']
));
});