Sentry-php: [2.0] Monolog

Created on 8 Mar 2019  路  14Comments  路  Source: getsentry/sentry-php

In sentry client v1 we were able to use the Raven_Breadcrumbs_MonologHandler handler. Is there a similar functionality in v2? There doesn't appear to be any documentation on this and it isn't listed as a removed feature either.

Confirmed Documentation Question

Most helpful comment

Hi, any ETA to create monolog handler for 2.0 version?

All 14 comments

There is no feature like that one in the v2. We are reasoning about adding it back and there is an open issue in Seldaek/monolog#1273 to track the feature request. In the meantime the only way to do it is to write yourself the handler

There doesn't appear to be any documentation on this and it isn't listed as a removed feature either.

It is here

It is here

Ah thanks, I missed that. I was reading the official docs and didn't notice anything.

@ste93cry Would you consider creating a repository under @getsentry organization as suggested in linked issue and maintaining it there?

@fmasa We will move the code into this package.

@HazAT Thank you, that will be great :+1:

Hi, any ETA to create monolog handler for 2.0 version?

I'm also waiting to upgrade until the monolog handler is bundled.

Hi, any ETA to create monolog handler for 2.0 version?

I've opened the PR that should fix this issue. If you have any suggestion they are welcome! Please note that following Semver the first version that will include such feature will be 2.1

@ste93cry is there anyway to use monolog handler to track breadcrumbs as it did with the old raven sdk?

getsentry/sentry-php#844 is still pending review/discussion and should implement such feature, but talking with @untitaker I found out that in other SDKs the decision was to avoid as much as possible pulling information from structured logging entries, so it may be that it won't be supported anymore out-of-the-box. Of course this doesn't means that it cannot be implemented in your own project. My suggestion until a final decision on the matter is taken is to decorate the handler and add the breadcrumbs to the scope using withScope or configureScope

Ok no problem. I implemented my own which seems to be working well in case anyone else wants to use it.

<?php

declare(strict_types=1);

use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger;
use Sentry\State\HubInterface;
use Sentry\State\Scope;
use Sentry\Breadcrumb;

/**
 * This Monolog handler logs every message to a Sentry's server using the given
 * hub instance.
 */
final class MonologBreadcrumbHandler extends AbstractProcessingHandler
{
    /**
     * @var HubInterface
     */
    private $hub;

    /**
     * Constructor.
     *
     * @param HubInterface $hub    The hub to which errors are reported
     * @param int          $level  The minimum logging level at which this
     *                             handler will be triggered
     * @param bool         $bubble Whether the messages that are handled can
     *                             bubble up the stack or not
     */
    public function __construct(HubInterface $hub, $level = Logger::DEBUG, bool $bubble = true)
    {
        $this->hub = $hub;

        parent::__construct($level, $bubble);
    }

    /**
     * {@inheritdoc}
     */
    protected function write(array $record): void
    {
      $level = $this->getSeverityFromLevel($record['level']);
      $message = $record['message'];
      $type = Breadcrumb::TYPE_DEFAULT;
      $category = 'monolog';
      $metadata = $record['context'];

      $breadcrumb = new Breadcrumb($level, $type, $category, $message, $metadata);
      $this->hub->addBreadcrumb($breadcrumb);
    }

    /**
     * Translates the Monolog level into the Sentry severity.
     *
     * @param int $level The Monolog log level
     *
     * @return Severity
     */
    private function getSeverityFromLevel(int $level): string
    {
        switch ($level) {
            case Logger::DEBUG:
                return Breadcrumb::LEVEL_DEBUG;
            case Logger::INFO:
            case Logger::NOTICE:
                return Breadcrumb::LEVEL_INFO;
            case Logger::WARNING:
                return Breadcrumb::LEVEL_WARNING;
            case Logger::ERROR:
                return Breadcrumb::LEVEL_ERROR;
            case Logger::CRITICAL:
            case Logger::ALERT:
            case Logger::EMERGENCY:
                return Breadcrumb::LEVEL_CRITICAL;
            default:
                return Breadcrumb::LEVEL_INFO;
        }
    }
}

using it like this:

        $hub = \Sentry\State\Hub::getCurrent();
        $sentryHandler = new MonologBreadcrumbHandler($hub);
        $logger->pushHandler($sentryHandler);

@esetnik I released a dedicated handler lib if you are interested -> https://github.com/B-Galati/monolog-sentry-handler

What's the status here? The monolog documentation says the handler in sentry-php is the one to use. But there is no mention of it anywhere in the sentry-php documentation.

FTR, #808 added it in 2.1.0 of this client.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iluuu1994 picture iluuu1994  路  7Comments

parichayawalia picture parichayawalia  路  3Comments

tinproject picture tinproject  路  4Comments

Quetzacoalt91 picture Quetzacoalt91  路  4Comments

Kleingeld picture Kleingeld  路  5Comments