Browse Source

Merge pull request #332 from PaulStatezny/rollbar-handler

Add Rollbar handler
Jordi Boggiano 12 years ago
parent
commit
fab2a2deef
3 changed files with 66 additions and 1 deletions
  1. 1 0
      README.mdown
  2. 2 1
      composer.json
  3. 63 0
      src/Monolog/Handler/RollbarHandler.php

+ 1 - 0
README.mdown

@@ -133,6 +133,7 @@ Handlers
 - _ZendMonitorHandler_: Logs records to the Zend Monitor present in Zend Server.
 - _ZendMonitorHandler_: Logs records to the Zend Monitor present in Zend Server.
 - _NewRelicHandler_: Logs records to a [NewRelic](http://newrelic.com/) application.
 - _NewRelicHandler_: Logs records to a [NewRelic](http://newrelic.com/) application.
 - _LogglyHandler_: Logs records to a [Loggly](http://www.loggly.com/) account.
 - _LogglyHandler_: Logs records to a [Loggly](http://www.loggly.com/) account.
+- _RollbarHandler_: Logs records to a [Rollbar](https://rollbar.com/) account.
 - _SyslogUdpHandler_: Logs records to a remote [Syslogd](http://www.rsyslog.com/) server.
 - _SyslogUdpHandler_: Logs records to a remote [Syslogd](http://www.rsyslog.com/) server.
 
 
 ### Logging in development
 ### Logging in development

+ 2 - 1
composer.json

@@ -31,7 +31,8 @@
         "ruflin/elastica": "Allow sending log messages to an Elastic Search server",
         "ruflin/elastica": "Allow sending log messages to an Elastic Search server",
         "ext-amqp": "Allow sending log messages to an AMQP server (1.0+ required)",
         "ext-amqp": "Allow sending log messages to an AMQP server (1.0+ required)",
         "ext-mongo": "Allow sending log messages to a MongoDB server",
         "ext-mongo": "Allow sending log messages to a MongoDB server",
-        "aws/aws-sdk-php": "Allow sending log messages to AWS services like DynamoDB"
+        "aws/aws-sdk-php": "Allow sending log messages to AWS services like DynamoDB",
+        "rollbar/rollbar": "Allow sending log messages to Rollbar"
     },
     },
     "autoload": {
     "autoload": {
         "psr-4": {"Monolog\\": "src/Monolog"}
         "psr-4": {"Monolog\\": "src/Monolog"}

+ 63 - 0
src/Monolog/Handler/RollbarHandler.php

@@ -0,0 +1,63 @@
+<?php
+
+namespace Monolog\Handler;
+
+use RollbarNotifier;
+use Exception;
+
+/**
+ * Sends errors to Rollbar
+ *
+ * @author Paul Statezny <paulstatezny@gmail.com>
+ */
+class RollbarHandler extends AbstractProcessingHandler
+{
+    /**
+     * Rollbar notifier
+     *
+     * @var RollbarNotifier
+     */
+    protected $rollbarNotifier;
+
+    /**
+     * @param RollbarNotifier  $rollbarNotifier RollbarNotifier object constructed with valid token
+     * @param integer          $level           The minimum logging level at which this handler will be triggered
+     * @param boolean          $bubble          Whether the messages that are handled can bubble up the stack or not
+     */
+    public function __construct(RollbarNotifier $rollbarNotifier, $level = Logger::ERROR, $bubble = true)
+    {
+        $this->rollbarNotifier = $rollbarNotifier;
+
+        parent::__construct($level, $bubble);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function write(array $record)
+    {
+        if (isset($record['context']['exception']) && $record['context']['exception'] instanceof Exception) {
+            $this->rollbarNotifier->report_exception($record['context']['exception']);
+        } else {
+            $extraData = array(
+                'level' => $record['level'],
+                'channel' => $record['channel'],
+                'datetime' => $record['datetime']->format('U'),
+            );
+
+            $this->rollbarNotifier->report_message(
+                $record['message'],
+                $record['level_name'],
+                array_merge($record['context'], $record['extra'], $extraData)
+            );
+        }
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function close()
+    {
+        $this->rollbarNotifier->flush();
+    }
+}