4 * This file is part of the Monolog package.
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Monolog\Handler;
19 * Sends errors to Rollbar
21 * If the context data contains a `payload` key, that is used as an array
22 * of payload options to RollbarNotifier's report_message/report_exception methods.
24 * Rollbar's context info will contain the context + extra keys from the log record
25 * merged, and then on top of that a few keys:
27 * - level (rollbar level name)
28 * - monolog_level (monolog level name, raw level, as rollbar only has 5 but monolog 8)
30 * - datetime (unix timestamp)
32 * @author Paul Statezny <paulstatezny@gmail.com>
34 class RollbarHandler extends AbstractProcessingHandler
39 * @var RollbarNotifier
41 protected $rollbarNotifier;
43 protected $levelMap = array(
44 Logger::DEBUG => 'debug',
45 Logger::INFO => 'info',
46 Logger::NOTICE => 'info',
47 Logger::WARNING => 'warning',
48 Logger::ERROR => 'error',
49 Logger::CRITICAL => 'critical',
50 Logger::ALERT => 'critical',
51 Logger::EMERGENCY => 'critical',
55 * Records whether any log records have been added since the last flush of the rollbar notifier
59 private $hasRecords = false;
61 protected $initialized = false;
64 * @param RollbarNotifier $rollbarNotifier RollbarNotifier object constructed with valid token
65 * @param int $level The minimum logging level at which this handler will be triggered
66 * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
68 public function __construct(RollbarNotifier $rollbarNotifier, $level = Logger::ERROR, $bubble = true)
70 $this->rollbarNotifier = $rollbarNotifier;
72 parent::__construct($level, $bubble);
78 protected function write(array $record)
80 if (!$this->initialized) {
81 // __destructor() doesn't get called on Fatal errors
82 register_shutdown_function(array($this, 'close'));
83 $this->initialized = true;
86 $context = $record['context'];
88 if (isset($context['payload'])) {
89 $payload = $context['payload'];
90 unset($context['payload']);
92 $context = array_merge($context, $record['extra'], array(
93 'level' => $this->levelMap[$record['level']],
94 'monolog_level' => $record['level_name'],
95 'channel' => $record['channel'],
96 'datetime' => $record['datetime']->format('U'),
99 if (isset($context['exception']) && $context['exception'] instanceof Exception) {
100 $exception = $context['exception'];
101 unset($context['exception']);
103 $this->rollbarNotifier->report_exception($exception, $context, $payload);
105 $this->rollbarNotifier->report_message(
113 $this->hasRecords = true;
116 public function flush()
118 if ($this->hasRecords) {
119 $this->rollbarNotifier->flush();
120 $this->hasRecords = false;
127 public function close()