monolog-bundle icon indicating copy to clipboard operation
monolog-bundle copied to clipboard

rollbar support for rollbar/rollbar-php rewrite latest version

Open cordoval opened this issue 7 years ago • 2 comments

There is no RollbarNotifier now in the latest version so it cannot be used with monolog

cordoval avatar Jun 05 '17 19:06 cordoval

See https://github.com/Seldaek/monolog/issues/980#issuecomment-309313868 - PR welcome if anyone wants, would be good to do this conditionally if rollbar 1.0+ can be detected in code somehow (maybe just checking if RollbarNotifier class exists as that was pre-1.0?)

Seldaek avatar Jun 19 '17 00:06 Seldaek

<?php

namespace Ghs\Application;

use Monolog\Handler\AbstractHandler;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
use Rollbar\RollbarLogger;

class RollbarHandler extends AbstractHandler
{
    /** @var RollbarLogger */
    private $logger;

    public function __construct(LoggerInterface $logger, $level = Logger::DEBUG, $bubble = true)
    {
        parent::__construct($level, $bubble);

        $this->logger = $logger;
    }

    public function handle(array $record)
    {
        if (!$this->isHandling($record)) {
            return false;
        }

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

        $this->logger->log(strtolower($record['level_name']), $record['message'], $record['context']);

        return false === $this->bubble;
    }
}

cordoval avatar Jun 19 '17 00:06 cordoval