Sentry-php: How to configure monolog handler

Created on 7 Jun 2019  路  5Comments  路  Source: getsentry/sentry-php

I have a problem with configuring sentry with monolog usage

in documentation example they use old raven client
https://docs.sentry.io/clients/php/integrations/#capturing-errors

$client = new Raven_Client('https://<key>@sentry.io/<project>');

$handler = new Monolog\Handler\RavenHandler($client);
$handler->setFormatter(new Monolog\Formatter\LineFormatter("%message% %context% %extra%\n"));

$monolog->pushHandler($handler);

In other part of documentation we use new client
i try to configure monolog with new client, but can't see any events in sentry

$arConfig = ['dsn' => 'https://[email protected]/1477408'];
$options = new Options($arConfig);
$transport = new HttpTransport($options, HttpAsyncClientDiscovery::find(), MessageFactoryDiscovery::find());
$builder = ClientBuilder::create($arConfig);
$builder->setTransport($transport);
Hub::getCurrent()->bindClient($builder->getClient());
$handlers[] = new \Sentry\Monolog\Handler(Hub::getCurrent(), Logger::ERROR);
$logger->pushHandler(new Monolog\Handler\WhatFailureGroupHandler($handlers));

Question

Most helpful comment

Please update your documentation properly. It's horrible. Why isn't there any working example of how to instantiate the Monolog handler correctly?

Sorry for my harsh words, but this is not just open source, but a company and we pay for it. I can expect a proper documentation and working examples for integrations with paid software.

All 5 comments

When you set a custom transport you have to take care of adding also all the plugins that make the authentication works in the HTTP client

https://github.com/getsentry/sentry-php/blob/e2d99f1cf0158f5758b46e5e461052703a752d09/src/ClientBuilder.php#L292-L295

This took me longer to figure out than I care to admit. It's been a while since your question, but perhaps it can help others searching.

Going with the basics, the new Monolog handler is used as follows:

$logger = new \Monolog\Logger('logger_name');
$client = \Sentry\ClientBuilder::create(['dsn' => SENTRY_DSN])->getClient();
$handler = new \Sentry\Monolog\Handler(new \Sentry\State\Hub($client));
$logger->pushHandler($handler);

Then send a message as follows:

$logger->error('Message', $context);

Context is passed along as an optional array:

['exception' => $e, 'extra' => ['some' => 'content'], 'tags' => ['sentry' => 'tags']]

Closing this since you solved the issue yourself. Feel free to reopen if you need more help

Please update your documentation properly. It's horrible. Why isn't there any working example of how to instantiate the Monolog handler correctly?

Sorry for my harsh words, but this is not just open source, but a company and we pay for it. I can expect a proper documentation and working examples for integrations with paid software.

getsentry/sentry-docs#1104 is tracking this issue and is waiting on a feature that will be available in 2.3 that will make it easier to instantiate and use the handler.

Sorry for my harsh words, but this is not just open source, but a company and we pay for it

I cannot speak for the company behind Sentry, but I agree that the documentation is for sure not optimal/well organized. However, as you said this is open source so as I contribute (and probably it's my fault to not have documented the features I developed) you can do the same by updating the documentation if you think that something needs adjustments :wink:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

brunowowk picture brunowowk  路  5Comments

realtebo picture realtebo  路  5Comments

iluuu1994 picture iluuu1994  路  7Comments

Nix-id picture Nix-id  路  5Comments

mitchhentges picture mitchhentges  路  3Comments