DataDog / documentation

The source for Datadog's documentation site.

Home Page:http://docs.datadoghq.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PHP: invalid Monolog snippet

gnumoksha opened this issue · comments

Documentation Page

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']
      ));
  });