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 a61482f..3e30dc4 100644 --- a/README.md +++ b/README.md @@ -16,12 +16,14 @@ 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 @@ -30,6 +32,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\RollbarLumenServiceProvider::class); +} +``` + Configuration ------------- @@ -47,6 +63,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/Facades/Rollbar.php b/src/Facades/Rollbar.php index c230c09..9ce9c36 100644 --- a/src/Facades/Rollbar.php +++ b/src/Facades/Rollbar.php @@ -1,4 +1,6 @@ -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 +142,4 @@ protected function parseLevel($level) throw new InvalidArgumentException('Invalid log level: ' . $level); } -} +} \ No newline at end of file diff --git a/src/RollbarLumenServiceProvider.php b/src/RollbarLumenServiceProvider.php new file mode 100644 index 0000000..3cd9239 --- /dev/null +++ b/src/RollbarLumenServiceProvider.php @@ -0,0 +1,56 @@ +app->configure('services'); + + $this->registerLibrary(); + } + + 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]); + $handler->getFormatter()->includeStacktraces(); + + return $handler; + }); + } + + protected function registerLogListener() + { + $this->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/src/RollbarServiceProvider.php b/src/RollbarServiceProvider.php index 7937fca..e78027d 100644 --- a/src/RollbarServiceProvider.php +++ b/src/RollbarServiceProvider.php @@ -1,9 +1,13 @@ -app; - - // Listen to log messages. - $app['log']->listen(function () use ($app) { - $args = func_get_args(); - - // Laravel 5.4 returns a MessageLogged instance only - if (count($args) == 1) { - $level = $args[0]->level; - $message = $args[0]->message; - $context = $args[0]->context; - } else { - $level = $args[0]; - $message = $args[1]; - $context = $args[2]; - } - - $app['Jenssegers\Rollbar\RollbarLogHandler']->log($level, $message, $context); - }); + $this->registerLibrary(); } /** - * Register the service provider. + * Bootstrap the application events. */ - public function register() + public function boot() + { + $this->registerLogListener(); + } + + protected function registerLibrary() { // 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; } - $app = $this->app; + $this->registerRollbarNotifier(); + + $this->registerRollbarLogHandler(); + + $this->registerErrorHandlers(); + } + protected function registerRollbarNotifier() + { $this->app->singleton('RollbarNotifier', function ($app) { // Default configuration. $defaults = [ @@ -63,35 +61,76 @@ public function register() $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'); + throw new InvalidArgumentException('Rollbar access token not configured.'); } Rollbar::$instance = $rollbar = new RollbarNotifier($config); - return $rollbar; }); + } + protected function registerRollbarLogHandler() + { $this->app->singleton('Jenssegers\Rollbar\RollbarLogHandler', function ($app) { $level = getenv('ROLLBAR_LEVEL') ?: $app['config']->get('services.rollbar.level', 'debug'); return new RollbarLogHandler($app['RollbarNotifier'], $app, $level); }); + } + protected function registerErrorHandlers() + { // Register the fatal error handler. - register_shutdown_function(function () use ($app) { - if (isset($app['RollbarNotifier'])) { - $app->make('RollbarNotifier'); - Rollbar::report_fatal_error(); + register_shutdown_function(function () { + if (isset($this->app['RollbarNotifier'])) { + $rollbar = $this->app->make('RollbarNotifier'); + + // Rollbar::report_fatal_error(); + + $this->app['RollbarNotifier']->flush(); } }); + } + + protected function registerLogListener() + { + $this->app['log']->listen(function () { + $args = func_get_args(); - // 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(); + // Laravel 5.4 returns a MessageLogged instance only. + if (count($args) == 1) { + $level = $args[0]->level; + $message = $args[0]->message; + $context = $args[0]->context; + } else { + $level = $args[0]; + $message = $args[1]; + $context = $args[2]; } + + $this->app['Jenssegers\Rollbar\RollbarLogHandler']->log($level, $message, $context); }); } -} +} \ No newline at end of file diff --git a/tests/RollbarLumenTest.php b/tests/RollbarLumenTest.php new file mode 100644 index 0000000..a301945 --- /dev/null +++ b/tests/RollbarLumenTest.php @@ -0,0 +1,10 @@ +