Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional data with Monolog #1014

Closed
enumag opened this issue May 7, 2020 · 6 comments
Closed

Additional data with Monolog #1014

enumag opened this issue May 7, 2020 · 6 comments

Comments

@enumag
Copy link
Contributor

enumag commented May 7, 2020

Some of my calls to Monolog contain additional data in the $context array. I need to get some of these data into the \Sentry\Event object. I want to use an event processor for that since it gets both the Event instance and the original $payload array (see #1010).

The issue is that the data I need don't make it into the $payload array at the moment. The Monolog Handler only copies the exception but the rest of $record['context'] is thrown away:

if (isset($record['context']['exception']) && $record['context']['exception'] instanceof \Throwable) {
$payload['exception'] = $record['context']['exception'];
}

Do I have to write my own Monolog Handler for this or can we improve the existing one in this library to pass the data into the payload?

@enumag
Copy link
Contributor Author

enumag commented May 7, 2020

Related to amphp/http-server#310.

@Jean85
Copy link
Collaborator

Jean85 commented May 7, 2020

Changing the handler was previously shot down due (and explained) in #844 & #848; TL;DR, we cannot assume what's inside Monolog context, so extracting info bu default could lead to unwanted stuff.

There's also https://github.com/B-Galati/monolog-sentry-handler that could help you in getting more information from Monolog.

Our handler is final, but you can surely use nested handler to avoid rewriting it.

@enumag
Copy link
Contributor Author

enumag commented May 11, 2020

Our handler is final, but you can surely use nested handler to avoid rewriting it.

I don't see how in this case.

@Jean85
Copy link
Collaborator

Jean85 commented May 11, 2020

In the same way that special handler do, decorating the original handler: https://seldaek.github.io/monolog/doc/02-handlers-formatters-processors.html#wrappers--special-handlers

@enumag
Copy link
Contributor Author

enumag commented May 11, 2020

I managed to get the data through the existing Handler by implementing Monolog\Processor\ProcessorInterface:

namespace App\Library\Monolog;

use Monolog\Processor\ProcessorInterface;

final class RequestExtraProcessor implements ProcessorInterface
{
	public function __invoke(array $record): array
	{
		if (array_key_exists('request', $record['context'])) {
			$record['context']['extra']['request'] = $record['context']['request'];
		}

		return $record;
	}
}

@enumag enumag closed this as completed May 11, 2020
@niklasnatter
Copy link

niklasnatter commented Jul 1, 2021

For completion; As far as I understand, the solution of registering a monolog processor described in #1014 (comment) does not work with new versions of this bundle, because the Handler implementation of the sentry/sentry package does not access $record['context']['extra']:

/**
* {@inheritdoc}
*/
protected function write(array $record): void
{
$event = Event::createEvent();
$event->setLevel(self::getSeverityFromLevel($record['level']));
$event->setMessage($record['message']);
$event->setLogger(sprintf('monolog.%s', $record['channel']));
$hint = new EventHint();
if (isset($record['context']['exception']) && $record['context']['exception'] instanceof \Throwable) {
$hint->exception = $record['context']['exception'];
}
$this->hub->withScope(function (Scope $scope) use ($record, $event, $hint): void {
$scope->setExtra('monolog.channel', $record['channel']);
$scope->setExtra('monolog.level', $record['level_name']);
$this->hub->captureEvent($event, $hint);
});
}

If you want to do this with new versions of this bundle, you need to decorate the monolog handler (or register your own handler) like described in #848 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants