From 6f7023547091c3679d0e618eb67bd7b1a19db89a Mon Sep 17 00:00:00 2001 From: Paul Statezny Date: Thu, 6 Mar 2014 10:57:37 -0700 Subject: [PATCH 1/7] Add Rollbar handler. --- src/Monolog/Handler/RollbarHandler.php | 56 ++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/Monolog/Handler/RollbarHandler.php diff --git a/src/Monolog/Handler/RollbarHandler.php b/src/Monolog/Handler/RollbarHandler.php new file mode 100644 index 00000000..6cfaf65b --- /dev/null +++ b/src/Monolog/Handler/RollbarHandler.php @@ -0,0 +1,56 @@ + + */ +class RollbarHandler extends AbstractProcessingHandler +{ + /** + * Rollbar notifier + * + * @var RollbarNotifier + */ + protected $rollbarNotifier; + + /** + * @param string $token post_server_item access token for the Rollbar project + * @param string $environment This can be set to any string + * @param string $root Directory your code is in; used for linking stack traces + * @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($token, $environment = 'production', $root = null, $level = Logger::ERROR, $bubble = true) + { + $this->rollbarNotifier = new RollbarNotifier(array( + 'access_token' => $token, + 'environment' => $environment, + 'root' => $root, + )); + + parent::__construct($level, $bubble); + } + + protected function write(array $record) + { + if (isset($record['context']) and isset($record['context']['exception'])) { + $this->rollbarNotifier->report_exception($record['context']['exception']); + } else { + $this->rollbarNotifier->report_message( + $record['message'], + $record['level_name'], + $record['extra'] + ); + } + } + + public function __destruct() + { + $this->rollbarNotifier->flush(); + } +} From 6734a4fbd184e86e1ce74325b622b3f7c4f4242b Mon Sep 17 00:00:00 2001 From: Paul Statezny Date: Thu, 6 Mar 2014 10:58:34 -0700 Subject: [PATCH 2/7] Add composer suggest for Rollbar package. Update documentation. --- README.mdown | 1 + composer.json | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) 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"} From 776a8944878e56cba0fc6a794c1a22f02b6feb1c Mon Sep 17 00:00:00 2001 From: Paul Statezny Date: Thu, 6 Mar 2014 11:40:41 -0700 Subject: [PATCH 3/7] Inject rollbar handler. Typecheck exceptions. Coding standard fix. --- src/Monolog/Handler/RollbarHandler.php | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/Monolog/Handler/RollbarHandler.php b/src/Monolog/Handler/RollbarHandler.php index 6cfaf65b..4297be85 100644 --- a/src/Monolog/Handler/RollbarHandler.php +++ b/src/Monolog/Handler/RollbarHandler.php @@ -3,6 +3,7 @@ namespace Monolog\Handler; use RollbarNotifier; +use Exception; /** * Sends errors to Rollbar @@ -19,26 +20,20 @@ class RollbarHandler extends AbstractProcessingHandler protected $rollbarNotifier; /** - * @param string $token post_server_item access token for the Rollbar project - * @param string $environment This can be set to any string - * @param string $root Directory your code is in; used for linking stack traces - * @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 + * @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($token, $environment = 'production', $root = null, $level = Logger::ERROR, $bubble = true) + public function __construct(RollbarNotifier $rollbarNotifier, $level = Logger::ERROR, $bubble = true) { - $this->rollbarNotifier = new RollbarNotifier(array( - 'access_token' => $token, - 'environment' => $environment, - 'root' => $root, - )); + $this->rollbarNotifier = $rollbarNotifier; parent::__construct($level, $bubble); } protected function write(array $record) { - if (isset($record['context']) and isset($record['context']['exception'])) { + if (isset($record['context']['exception']) && $record['context']['exception'] instanceof Exception) { $this->rollbarNotifier->report_exception($record['context']['exception']); } else { $this->rollbarNotifier->report_message( From 2bb729d60b1c98fa2ef79afa052b736ca7801137 Mon Sep 17 00:00:00 2001 From: Paul Statezny Date: Thu, 6 Mar 2014 11:42:52 -0700 Subject: [PATCH 4/7] Flush roll bar notifier on close rather than destruct. Inherit DocBlocks. --- src/Monolog/Handler/RollbarHandler.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Monolog/Handler/RollbarHandler.php b/src/Monolog/Handler/RollbarHandler.php index 4297be85..7a1867ec 100644 --- a/src/Monolog/Handler/RollbarHandler.php +++ b/src/Monolog/Handler/RollbarHandler.php @@ -31,6 +31,9 @@ class RollbarHandler extends AbstractProcessingHandler parent::__construct($level, $bubble); } + /** + * {@inheritdoc} + */ protected function write(array $record) { if (isset($record['context']['exception']) && $record['context']['exception'] instanceof Exception) { @@ -44,7 +47,10 @@ class RollbarHandler extends AbstractProcessingHandler } } - public function __destruct() + /** + * {@inheritdoc} + */ + public function close() { $this->rollbarNotifier->flush(); } From 595c6fc8af4310b5a4a5827f03b2fe6d584a4144 Mon Sep 17 00:00:00 2001 From: Paul Statezny Date: Thu, 6 Mar 2014 14:18:57 -0700 Subject: [PATCH 5/7] Include context data as extra data sent to Rollbar. --- src/Monolog/Handler/RollbarHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Monolog/Handler/RollbarHandler.php b/src/Monolog/Handler/RollbarHandler.php index 7a1867ec..734cbd8d 100644 --- a/src/Monolog/Handler/RollbarHandler.php +++ b/src/Monolog/Handler/RollbarHandler.php @@ -42,7 +42,7 @@ class RollbarHandler extends AbstractProcessingHandler $this->rollbarNotifier->report_message( $record['message'], $record['level_name'], - $record['extra'] + array_merge($record['context'], $record['extra']) ); } } From fbf654f31d1dabfb93da9615196b08a2ec33ac84 Mon Sep 17 00:00:00 2001 From: Paul Statezny Date: Thu, 6 Mar 2014 14:32:04 -0700 Subject: [PATCH 6/7] Include level, channel, and datetime in extra data sent to Rollbar. --- src/Monolog/Handler/RollbarHandler.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Monolog/Handler/RollbarHandler.php b/src/Monolog/Handler/RollbarHandler.php index 734cbd8d..ca4b414f 100644 --- a/src/Monolog/Handler/RollbarHandler.php +++ b/src/Monolog/Handler/RollbarHandler.php @@ -39,10 +39,16 @@ class RollbarHandler extends AbstractProcessingHandler 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'], + ); + $this->rollbarNotifier->report_message( $record['message'], $record['level_name'], - array_merge($record['context'], $record['extra']) + array_merge($record['context'], $record['extra'], $extraData) ); } } From 94c90ade893d297d5e9d7df443f7d6f1fc410dae Mon Sep 17 00:00:00 2001 From: Paul Statezny Date: Thu, 6 Mar 2014 14:37:59 -0700 Subject: [PATCH 7/7] Send datetime as a string so it can be converted to JSON. --- src/Monolog/Handler/RollbarHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Monolog/Handler/RollbarHandler.php b/src/Monolog/Handler/RollbarHandler.php index ca4b414f..d7738215 100644 --- a/src/Monolog/Handler/RollbarHandler.php +++ b/src/Monolog/Handler/RollbarHandler.php @@ -42,7 +42,7 @@ class RollbarHandler extends AbstractProcessingHandler $extraData = array( 'level' => $record['level'], 'channel' => $record['channel'], - 'datetime' => $record['datetime'], + 'datetime' => $record['datetime']->format('U'), ); $this->rollbarNotifier->report_message(