diff --git a/README.mdown b/README.mdown index a519b9f8..3919776a 100644 --- a/README.mdown +++ b/README.mdown @@ -133,6 +133,7 @@ Handlers - _ZendMonitorHandler_: Logs records to the Zend Monitor present in Zend Server. - _NewRelicHandler_: Logs records to a [NewRelic](http://newrelic.com/) application. - _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. ### Logging in development diff --git a/composer.json b/composer.json index 303aa7d1..f21a4245 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,8 @@ "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-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": { "psr-4": {"Monolog\\": "src/Monolog"} diff --git a/src/Monolog/Handler/RollbarHandler.php b/src/Monolog/Handler/RollbarHandler.php new file mode 100644 index 00000000..d7738215 --- /dev/null +++ b/src/Monolog/Handler/RollbarHandler.php @@ -0,0 +1,63 @@ + + */ +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(); + } +}