From f02e64dfef7d606f03fedead02f943d516fd2bf4 Mon Sep 17 00:00:00 2001 From: Giampaolo Falqui Date: Mon, 23 May 2016 13:27:29 +0200 Subject: [PATCH 01/10] Adds Lumen 5.x support --- .gitignore | 1 + README.md | 68 +++++++++++++------- src/RollbarLumenServiceProvider.php | 97 +++++++++++++++++++++++++++++ tests/RollbarLumenTest.php | 10 +++ 4 files changed, 154 insertions(+), 22 deletions(-) create mode 100644 src/RollbarLumenServiceProvider.php create mode 100644 tests/RollbarLumenTest.php diff --git a/.gitignore b/.gitignore index fc7ea17..85c0bc3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /vendor composer.lock .DS_Store +*.sublime* \ No newline at end of file diff --git a/README.md b/README.md index c76ef8f..a61482f 100644 --- a/README.md +++ b/README.md @@ -12,21 +12,35 @@ Installation Install using composer: - composer require jenssegers/rollbar +``` +composer require jenssegers/rollbar +``` Add the service provider to the `'providers'` array in `config/app.php`: - Jenssegers\Rollbar\RollbarServiceProvider::class, +```php +Jenssegers\Rollbar\RollbarServiceProvider::class, +``` + +If you only want to enable Rollbar reporting for certain environments you can conditionally load the service provider in your `AppServiceProvider`: + +```php +if ($this->app->environment('production')) { + $this->app->register(\Jenssegers\Rollbar\RollbarServiceProvider::class); +} +``` Configuration ------------- This package supports configuration through the services configuration file located in `config/services.php`. All configuration variables will be directly passed to Rollbar: - 'rollbar' => array( - 'access_token' => env('ROLLBAR_TOKEN'), - 'level' => env('ROLLBAR_LEVEL'), - ), +```php +'rollbar' => [ + 'access_token' => env('ROLLBAR_TOKEN'), + 'level' => env('ROLLBAR_LEVEL'), +], +``` The level variable defines the minimum log level at which log messages are sent to Rollbar. For development you could set this either to `debug` to send all log messages, or to `none` to sent no messages at all. For production you could set this to `error` so that all info and debug messages are ignored. @@ -35,23 +49,29 @@ Usage To automatically monitor exceptions, simply use the `Log` facade in your error handler in `app/Exceptions/Handler.php`: - public function report(Exception $e) - { - \Log::error($e); +```php +public function report(Exception $e) +{ + \Log::error($e); + return parent::report($e); +} +``` - return parent::report($e); - } For Laravel 4 installations, this is located in `app/start/global.php`: - App::error(function(Exception $exception, $code) - { - Log::error($exception); - }); +```php +App::error(function(Exception $exception, $code) +{ + Log::error($exception); +}); +``` Your other log messages will also be sent to Rollbar: - \Log::debug('Here is some debug information'); +```php +\Log::debug('Here is some debug information'); +``` *NOTE*: Fatal exceptions will always be sent to Rollbar. @@ -59,12 +79,16 @@ Your other log messages will also be sent to Rollbar: You can pass user information as context like this: - \Log::error('Something went wrong', [ - 'person' => ['id' => 123, 'username' => 'John Doe', 'email' => 'john@doe.com'] - ]); +```php +\Log::error('Something went wrong', [ + 'person' => ['id' => 123, 'username' => 'John Doe', 'email' => 'john@doe.com'] +]); +``` Or pass some extra information: - \Log::warning('Something went wrong', [ - 'download_size' => 3432425235 - ]); +```php +\Log::warning('Something went wrong', [ + 'download_size' => 3432425235 +]); +``` diff --git a/src/RollbarLumenServiceProvider.php b/src/RollbarLumenServiceProvider.php new file mode 100644 index 0000000..2d64b73 --- /dev/null +++ b/src/RollbarLumenServiceProvider.php @@ -0,0 +1,97 @@ +app->configure('services'); + + // Don't register rollbar if it is not configured. + if (! getenv('ROLLBAR_TOKEN') and ! $this->app['config']->get('services.rollbar')) { + return; + } + + $app = $this->app; + + $app[RollbarNotifier::class] = $app->share(function ($app) { + + // Default configuration. + $defaults = [ + 'environment' => $app->environment(), + 'root' => base_path(), + ]; + + $config = array_merge($defaults, $app['config']->get('services.rollbar', [])); + + $config['access_token'] = getenv('ROLLBAR_TOKEN') ?: $app['config']->get('services.rollbar.access_token'); + + if (empty($config['access_token'])) { + throw new InvalidArgumentException('Rollbar access token not configured'); + } + + Rollbar::$instance = $rollbar = new RollbarNotifier($config); + + return $rollbar; + }); + + $app[RollbarLogHandler::class] = $app->share(function ($app) { + $level = getenv('ROLLBAR_LEVEL') ?: $app['config']->get('services.rollbar.level', 'debug'); + + $handler = app(RollbarHandler::class, [$this->app[RollbarNotifier::class], $level]); + + return $handler; + }); + + // Register the fatal error handler. + register_shutdown_function(function () use ($app) { + if (isset($app[Rollbar::class])) { + $app->make(Rollbar::class); + Rollbar::report_fatal_error(); + } + }); + + // If the Rollbar client was resolved, then there is a possibility that there + // are unsent error messages in the internal queue, so let's flush them. + register_shutdown_function(function () use ($app) { + if (isset($app[Rollbar::class])) { + $app[Rollbar::class]->flush(); + } + }); + } + + public function boot() + { + $app = $this->app; + + // Listen to log messages. + $app['log']->pushHandler( + app(RollbarLogHandler::class, [ + $this->app[Rollbar::class] + ]) + ); + } + + public function provides() + { + return [ + RollbarLogHandler::class + ]; + } +} \ No newline at end of file diff --git a/tests/RollbarLumenTest.php b/tests/RollbarLumenTest.php new file mode 100644 index 0000000..e00c81e --- /dev/null +++ b/tests/RollbarLumenTest.php @@ -0,0 +1,10 @@ + Date: Mon, 1 Aug 2016 10:47:30 +0200 Subject: [PATCH 02/10] Refactors ServiceProvider and improves code redundancy --- README.md | 20 +++++++++- src/RollbarLumenServiceProvider.php | 61 +++++++++-------------------- src/RollbarServiceProvider.php | 58 +++++++++++++++++---------- 3 files changed, 73 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index a61482f..fc64351 100644 --- a/README.md +++ b/README.md @@ -16,13 +16,13 @@ Install using composer: composer require jenssegers/rollbar ``` +### Laravel 5.x + Add the service provider to the `'providers'` array in `config/app.php`: ```php Jenssegers\Rollbar\RollbarServiceProvider::class, ``` - -If you only want to enable Rollbar reporting for certain environments you can conditionally load the service provider in your `AppServiceProvider`: ```php if ($this->app->environment('production')) { @@ -30,6 +30,20 @@ if ($this->app->environment('production')) { } ``` +### Lumen 5.x + +Register the service provider in `bootstrap/app.php`: + +```php +$app->register(Jenssegers\Rollbar\RollbarLumenServiceProvider::class); +``` + +```php +if ($app->environment('production')) { + $app->register(\Jenssegers\Rollbar\RollbarServiceProvider::class); +} +``` + Configuration ------------- @@ -47,6 +61,8 @@ The level variable defines the minimum log level at which log messages are sent Usage ----- +Lumen Service Provider automatically monitors exceptions, so the following setup is only for Laravel. + To automatically monitor exceptions, simply use the `Log` facade in your error handler in `app/Exceptions/Handler.php`: ```php diff --git a/src/RollbarLumenServiceProvider.php b/src/RollbarLumenServiceProvider.php index 2d64b73..61cef76 100644 --- a/src/RollbarLumenServiceProvider.php +++ b/src/RollbarLumenServiceProvider.php @@ -1,13 +1,13 @@ app; - - $app[RollbarNotifier::class] = $app->share(function ($app) { - - // Default configuration. - $defaults = [ - 'environment' => $app->environment(), - 'root' => base_path(), - ]; - - $config = array_merge($defaults, $app['config']->get('services.rollbar', [])); + $this->registerRollbarNotifier(); - $config['access_token'] = getenv('ROLLBAR_TOKEN') ?: $app['config']->get('services.rollbar.access_token'); + $this->registerRollbarLogHandler(); - if (empty($config['access_token'])) { - throw new InvalidArgumentException('Rollbar access token not configured'); - } - - Rollbar::$instance = $rollbar = new RollbarNotifier($config); + $this->registerErrorHandlers(); + } - return $rollbar; - }); + /** + * Bootstrap the application events. + */ + public function boot() + { + $this->registerLogListener(); + } - $app[RollbarLogHandler::class] = $app->share(function ($app) { + protected function registerRollbarLogHandler() + { + $this->app[RollbarLogHandler::class] = $this->app->share(function ($app) { $level = getenv('ROLLBAR_LEVEL') ?: $app['config']->get('services.rollbar.level', 'debug'); $handler = app(RollbarHandler::class, [$this->app[RollbarNotifier::class], $level]); return $handler; }); - - // Register the fatal error handler. - register_shutdown_function(function () use ($app) { - if (isset($app[Rollbar::class])) { - $app->make(Rollbar::class); - Rollbar::report_fatal_error(); - } - }); - - // If the Rollbar client was resolved, then there is a possibility that there - // are unsent error messages in the internal queue, so let's flush them. - register_shutdown_function(function () use ($app) { - if (isset($app[Rollbar::class])) { - $app[Rollbar::class]->flush(); - } - }); } - public function boot() + protected function registerLogListener() { - $app = $this->app; - - // Listen to log messages. - $app['log']->pushHandler( + $this->app['log']->pushHandler( app(RollbarLogHandler::class, [ $this->app[Rollbar::class] ]) diff --git a/src/RollbarServiceProvider.php b/src/RollbarServiceProvider.php index fdace1e..8b34019 100644 --- a/src/RollbarServiceProvider.php +++ b/src/RollbarServiceProvider.php @@ -1,6 +1,7 @@ app; - - // Listen to log messages. - $app['log']->listen(function ($level, $message, $context) use ($app) { - $app['Jenssegers\Rollbar\RollbarLogHandler']->log($level, $message, $context); - }); - } - /** * Register the service provider. */ @@ -37,9 +25,24 @@ public function register() return; } - $app = $this->app; + $this->registerRollbarNotifier(); + + $this->registerRollbarLogHandler(); + + $this->registerErrorHandlers(); + } + + /** + * Bootstrap the application events. + */ + public function boot() + { + $this->registerLogListener(); + } - $this->app['RollbarNotifier'] = $this->app->share(function ($app) { + protected function registerRollbarNotifier() + { + $app[RollbarNotifier::class] = $this->app->share(function ($app) { // Default configuration. $defaults = [ 'environment' => $app->environment(), @@ -58,17 +61,23 @@ public function register() return $rollbar; }); + } - $this->app['Jenssegers\Rollbar\RollbarLogHandler'] = $this->app->share(function ($app) { + protected function registerRollbarLogHandler() + { + $this->app[RollbarLogHandler::class] = $this->app->share(function ($app) { $level = getenv('ROLLBAR_LEVEL') ?: $app['config']->get('services.rollbar.level', 'debug'); - return new RollbarLogHandler($app['RollbarNotifier'], $app, $level); + return new RollbarLogHandler($app[RollbarNotifier::class], $app, $level); }); + } + protected function registerErrorHandlers() + { // Register the fatal error handler. register_shutdown_function(function () use ($app) { - if (isset($app['RollbarNotifier'])) { - $app->make('RollbarNotifier'); + if (isset($this->app[RollbarNotifier::class])) { + $this->app->make(RollbarNotifier::class); Rollbar::report_fatal_error(); } }); @@ -76,9 +85,16 @@ public function register() // If the Rollbar client was resolved, then there is a possibility that there // are unsent error messages in the internal queue, so let's flush them. register_shutdown_function(function () use ($app) { - if (isset($app['RollbarNotifier'])) { - $app['RollbarNotifier']->flush(); + if (isset($this->app[RollbarNotifier::class])) { + $this->app[RollbarNotifier::class]->flush(); } }); } + + protected function registerLogListener() + { + $this->app['log']->listen(function ($level, $message, $context) use ($app) { + $app[RollbarLogHandler::class]->log($level, $message, $context); + }); + } } From 36b574d40a3e44dbb1638649c817109c009f3a36 Mon Sep 17 00:00:00 2001 From: Giampaolo Falqui Date: Mon, 1 Aug 2016 11:06:34 +0200 Subject: [PATCH 03/10] Fixes scope errors --- src/RollbarServiceProvider.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/RollbarServiceProvider.php b/src/RollbarServiceProvider.php index 8b34019..a9e3443 100644 --- a/src/RollbarServiceProvider.php +++ b/src/RollbarServiceProvider.php @@ -42,7 +42,7 @@ public function boot() protected function registerRollbarNotifier() { - $app[RollbarNotifier::class] = $this->app->share(function ($app) { + $this->app[RollbarNotifier::class] = $this->app->share(function ($app) { // Default configuration. $defaults = [ 'environment' => $app->environment(), @@ -54,7 +54,7 @@ protected function registerRollbarNotifier() $config['access_token'] = getenv('ROLLBAR_TOKEN') ?: $app['config']->get('services.rollbar.access_token'); if (empty($config['access_token'])) { - throw new InvalidArgumentException('Rollbar access token not configured'); + throw new InvalidArgumentException('Rollbar access token not configured.'); } Rollbar::$instance = $rollbar = new RollbarNotifier($config); @@ -75,7 +75,7 @@ protected function registerRollbarLogHandler() protected function registerErrorHandlers() { // Register the fatal error handler. - register_shutdown_function(function () use ($app) { + register_shutdown_function(function () { if (isset($this->app[RollbarNotifier::class])) { $this->app->make(RollbarNotifier::class); Rollbar::report_fatal_error(); @@ -84,7 +84,7 @@ protected function registerErrorHandlers() // If the Rollbar client was resolved, then there is a possibility that there // are unsent error messages in the internal queue, so let's flush them. - register_shutdown_function(function () use ($app) { + register_shutdown_function(function () { if (isset($this->app[RollbarNotifier::class])) { $this->app[RollbarNotifier::class]->flush(); } @@ -97,4 +97,4 @@ protected function registerLogListener() $app[RollbarLogHandler::class]->log($level, $message, $context); }); } -} +} \ No newline at end of file From 1242655ab02a58b66b233da7b9201267f46f55fd Mon Sep 17 00:00:00 2001 From: Giampaolo Falqui Date: Mon, 1 Aug 2016 11:15:38 +0200 Subject: [PATCH 04/10] Fixes StyleCI checks --- src/RollbarLogHandler.php | 6 +++--- src/RollbarLumenServiceProvider.php | 6 ++---- src/RollbarServiceProvider.php | 6 +++--- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/RollbarLogHandler.php b/src/RollbarLogHandler.php index c014f46..f90816c 100644 --- a/src/RollbarLogHandler.php +++ b/src/RollbarLogHandler.php @@ -1,11 +1,11 @@ Date: Wed, 14 Sep 2016 12:09:51 +0200 Subject: [PATCH 05/10] Adds StackTrace within a reported error --- src/RollbarLumenServiceProvider.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/RollbarLumenServiceProvider.php b/src/RollbarLumenServiceProvider.php index 349f6de..0978a84 100644 --- a/src/RollbarLumenServiceProvider.php +++ b/src/RollbarLumenServiceProvider.php @@ -47,6 +47,7 @@ protected function registerRollbarLogHandler() $level = getenv('ROLLBAR_LEVEL') ?: $app['config']->get('services.rollbar.level', 'debug'); $handler = app(RollbarHandler::class, [$this->app[RollbarNotifier::class], $level]); + $handler->getFormatter()->includeStacktraces(); return $handler; }); From ff5a04bc2e8044416352cdd67c558a8d371b4fcb Mon Sep 17 00:00:00 2001 From: Giampaolo Falqui Date: Wed, 14 Sep 2016 12:41:09 +0200 Subject: [PATCH 06/10] Aggiunge utente collegato nel context di logging per Rollbar --- src/RollbarServiceProvider.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/RollbarServiceProvider.php b/src/RollbarServiceProvider.php index 9c57af8..5093bbd 100644 --- a/src/RollbarServiceProvider.php +++ b/src/RollbarServiceProvider.php @@ -94,6 +94,15 @@ protected function registerErrorHandlers() protected function registerLogListener() { $this->app['log']->listen(function ($level, $message, $context) use ($app) { + + if ($user = \Auth::user()) { + $context['person'] = [ + 'id' => $user->id, + 'username' => $user->username, + 'email' => $user->email + ]; + } + $app[RollbarLogHandler::class]->log($level, $message, $context); }); } From ca66cd893b908e4402e121ead7d93d46e5484503 Mon Sep 17 00:00:00 2001 From: Giampaolo Falqui Date: Wed, 11 Jan 2017 17:07:06 +0100 Subject: [PATCH 07/10] Improvements to support better Lumen --- src/RollbarLogHandler.php | 4 ++-- src/RollbarServiceProvider.php | 36 ++++++++++++++++++++++++---------- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/RollbarLogHandler.php b/src/RollbarLogHandler.php index f90816c..d9aefe9 100644 --- a/src/RollbarLogHandler.php +++ b/src/RollbarLogHandler.php @@ -75,7 +75,7 @@ public function log($level, $message, array $context = []) $context = $this->addContext($context); - if ($message instanceof Exception) { + if ($message instanceof Exception || $message instanceof Throwable) { $this->rollbar->report_exception($message, null, $context); } else { $this->rollbar->report_message($message, $level, $context); @@ -140,4 +140,4 @@ protected function parseLevel($level) throw new InvalidArgumentException('Invalid log level: ' . $level); } -} +} \ No newline at end of file diff --git a/src/RollbarServiceProvider.php b/src/RollbarServiceProvider.php index 5093bbd..a3fa298 100644 --- a/src/RollbarServiceProvider.php +++ b/src/RollbarServiceProvider.php @@ -4,6 +4,7 @@ use RollbarNotifier; use InvalidArgumentException; use Illuminate\Support\ServiceProvider; +use Illuminate\Contracts\Auth\Guard; use Jenssegers\Rollbar\RollbarLogHandler; class RollbarServiceProvider extends ServiceProvider @@ -21,7 +22,7 @@ class RollbarServiceProvider extends ServiceProvider public function register() { // Don't register rollbar if it is not configured. - if (! getenv('ROLLBAR_TOKEN') and ! $this->app['config']->get('services.rollbar')) { + if (! getenv('ROLLBAR_TOKEN') && ! $this->app['config']->get('services.rollbar')) { return; } @@ -53,12 +54,32 @@ protected function registerRollbarNotifier() $config['access_token'] = getenv('ROLLBAR_TOKEN') ?: $app['config']->get('services.rollbar.access_token'); + if (is_callable($app['auth']->userResolver())) { + $config['person_fn'] = function () use ($app, $config) { + $user = @call_user_func($app['auth']->userResolver()); + + $person = [ + 'id' => $user->id + ]; + + if (array_key_exists('person_attributes', $config)) { + foreach ($config['person_attributes'] as $name => $value) { + $person[(is_string($name) && is_string($value)) ? $name : $value] = + array_reduce(explode('.', $value), function ($o, $p) { + return $o->$p; + }, $user); + } + } + + return $person; + }; + } + if (empty($config['access_token'])) { throw new InvalidArgumentException('Rollbar access token not configured.'); } Rollbar::$instance = $rollbar = new RollbarNotifier($config); - return $rollbar; }); } @@ -77,15 +98,10 @@ protected function registerErrorHandlers() // Register the fatal error handler. register_shutdown_function(function () { if (isset($this->app[RollbarNotifier::class])) { - $this->app->make(RollbarNotifier::class); - Rollbar::report_fatal_error(); - } - }); + $rollbar = $this->app->make(RollbarNotifier::class); + + // Rollbar::report_fatal_error(); - // If the Rollbar client was resolved, then there is a possibility that there - // are unsent error messages in the internal queue, so let's flush them. - register_shutdown_function(function () { - if (isset($this->app[RollbarNotifier::class])) { $this->app[RollbarNotifier::class]->flush(); } }); From 9ddc8f434043ea9f3d0d47878f387b31cd38779b Mon Sep 17 00:00:00 2001 From: Giampaolo Falqui Date: Mon, 15 May 2017 18:10:44 +0200 Subject: [PATCH 08/10] Small hack to remove empty class from test suite --- tests/RollbarLumenTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/RollbarLumenTest.php b/tests/RollbarLumenTest.php index e00c81e..a301945 100644 --- a/tests/RollbarLumenTest.php +++ b/tests/RollbarLumenTest.php @@ -1,6 +1,6 @@ Date: Mon, 15 May 2017 18:17:23 +0200 Subject: [PATCH 09/10] Adds again support for PHP 5.4 --- src/RollbarServiceProvider.php | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/src/RollbarServiceProvider.php b/src/RollbarServiceProvider.php index 5826b26..e26b664 100644 --- a/src/RollbarServiceProvider.php +++ b/src/RollbarServiceProvider.php @@ -50,7 +50,7 @@ protected function registerLibrary() protected function registerRollbarNotifier() { - $this->app->singleton(RollbarNotifier::class, function ($app) { + $this->app->singleton('RollbarNotifier', function ($app) { // Default configuration. $defaults = [ 'environment' => $app->environment(), @@ -93,10 +93,10 @@ protected function registerRollbarNotifier() protected function registerRollbarLogHandler() { - $this->app->singleton(RollbarLogHandler::class, function ($app) { + $this->app->singleton('Jenssegers\Rollbar\RollbarLogHandler', function ($app) { $level = getenv('ROLLBAR_LEVEL') ?: $app['config']->get('services.rollbar.level', 'debug'); - return new RollbarLogHandler($app[RollbarNotifier::class], $app, $level); + return new RollbarLogHandler($app['RollbarNotifier'], $app, $level); }); } @@ -104,12 +104,12 @@ protected function registerErrorHandlers() { // Register the fatal error handler. register_shutdown_function(function () { - if (isset($this->app[RollbarNotifier::class])) { - $rollbar = $this->app->make(RollbarNotifier::class); + if (isset($this->app['RollbarNotifier'])) { + $rollbar = $this->app->make('RollbarNotifier'); // Rollbar::report_fatal_error(); - $this->app[RollbarNotifier::class]->flush(); + $this->app['RollbarNotifier']->flush(); } }); } @@ -130,20 +130,7 @@ protected function registerLogListener() $context = $args[2]; } - $this->app[RollbarLogHandler::class]->log($level, $message, $context); + $this->app['Jenssegers\Rollbar\RollbarLogHandler']->log($level, $message, $context); }); - - /* $this->app['log']->listen(function ($level, $message, $context) use ($app) { - - if ($user = \Auth::user()) { - $context['person'] = [ - 'id' => $user->id, - 'username' => $user->username, - 'email' => $user->email - ]; - } - - $app[RollbarLogHandler::class]->log($level, $message, $context); - }); */ } -} +} \ No newline at end of file From 92f2bef011eccb7d0e0c6daf51d5d521c6e83de7 Mon Sep 17 00:00:00 2001 From: Giampaolo Falqui Date: Mon, 15 May 2017 18:35:58 +0200 Subject: [PATCH 10/10] Fixes is_callable for PHP <= 5.4 --- src/RollbarServiceProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/RollbarServiceProvider.php b/src/RollbarServiceProvider.php index e26b664..e78027d 100644 --- a/src/RollbarServiceProvider.php +++ b/src/RollbarServiceProvider.php @@ -61,7 +61,7 @@ protected function registerRollbarNotifier() $config['access_token'] = getenv('ROLLBAR_TOKEN') ?: $app['config']->get('services.rollbar.access_token'); - if (is_callable($app['auth']->userResolver())) { + if (is_callable([$app['auth'], 'userResolver'])) { $config['person_fn'] = function () use ($app, $config) { $user = @call_user_func($app['auth']->userResolver());