Skip to content

[Monolog] Handler not including context #870

@apapsch

Description

@apapsch

We have many log calls that are structured in the suggested way, i.e. context is an array with arbitrary fields:

                    $this->logger->error('Could not find channel of order', [
                        'order_id' => $orderId,
                        'channel_id' => $channelId,
                    ]);

Up until this week, we were still using Sentry 1.10 and the deprecated handler from Monolog upstream. The handler was configured with a LineFormatter and the line format %message% %context% %extra%. Sentry received messages like:

Could not find channel of order {"order_id": "FOO-1", "channel_id": "42"} []

Now we upgraded to Sentry 2.x and switched to the Sentry\Monolog\Handler. The message is now:

Could not find channel of order

The context is missing and also cannot be found under "Additional Data". Looking in the source of the integrated handler, the reasons become clear:

  • l. 49: The Sentry field message ist set using the Monolog field message, which ignores the formatted field set by the LineFormatter.
  • The arbitary context fields are not set in the Sentry scope.

The old behavior with the context added to the message as JSON object was not ideal, because Sentry would have had a hard time automatically merging the issues, resulting in many separate issues that needed manual merging. Therefore I would send a PR that adds the arbitrary context fields to the Sentry scope via setExtra.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions