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

[2.0] Monolog #790

Closed
esetnik opened this issue Mar 8, 2019 · 14 comments · Fixed by #808
Closed

[2.0] Monolog #790

esetnik opened this issue Mar 8, 2019 · 14 comments · Fixed by #808

Comments

@esetnik
Copy link

esetnik commented Mar 8, 2019

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.

@ste93cry
Copy link
Collaborator

ste93cry commented Mar 8, 2019

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

@esetnik
Copy link
Author

esetnik commented Mar 8, 2019

It is here

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

@fmasa
Copy link

fmasa commented Mar 11, 2019

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

@HazAT
Copy link
Member

HazAT commented Mar 11, 2019

@fmasa We will move the code into this package.

@fmasa
Copy link

fmasa commented Mar 11, 2019

@HazAT Thank you, that will be great 👍

@piotrantosik
Copy link
Contributor

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

@esetnik
Copy link
Author

esetnik commented Apr 18, 2019

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

@ste93cry ste93cry modified the milestones: 2.0, 2.1 Apr 22, 2019
@ste93cry
Copy link
Collaborator

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

@esetnik
Copy link
Author

esetnik commented Jul 9, 2019

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

@ste93cry
Copy link
Collaborator

ste93cry commented Jul 9, 2019

#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

@esetnik
Copy link
Author

esetnik commented Jul 9, 2019

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);

@B-Galati
Copy link
Contributor

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

@fuzzy76
Copy link

fuzzy76 commented Mar 23, 2020

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.

@Jean85
Copy link
Collaborator

Jean85 commented Mar 24, 2020

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

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

Successfully merging a pull request may close this issue.

8 participants