Skip to content
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor
composer.lock
.DS_Store
*.sublime*
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
-------------

Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/Facades/Rollbar.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace Jenssegers\Rollbar\Facades;
<?php

namespace Jenssegers\Rollbar\Facades;

use Illuminate\Support\Facades\Facade;

Expand Down
14 changes: 8 additions & 6 deletions src/RollbarLogHandler.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php namespace Jenssegers\Rollbar;
<?php

namespace Jenssegers\Rollbar;

use Exception;
use Illuminate\Foundation\Application;
use InvalidArgumentException;
use Monolog\Logger as Monolog;
use Psr\Log\AbstractLogger;
use RollbarNotifier;
use Psr\Log\AbstractLogger;
use Monolog\Logger as Monolog;
use Illuminate\Foundation\Application;

class RollbarLogHandler extends AbstractLogger
{
Expand Down Expand Up @@ -75,7 +77,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);
Expand Down Expand Up @@ -140,4 +142,4 @@ protected function parseLevel($level)

throw new InvalidArgumentException('Invalid log level: ' . $level);
}
}
}
56 changes: 56 additions & 0 deletions src/RollbarLumenServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Jenssegers\Rollbar;

use Rollbar;
use RollbarNotifier;
use Monolog\Handler\RollbarHandler;
use Jenssegers\Rollbar\RollbarLogHandler;

class RollbarLumenServiceProvider extends RollbarServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;

/**
* Register the service provider.
*/
public function register()
{
$this->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
];
}
}
117 changes: 78 additions & 39 deletions src/RollbarServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<?php namespace Jenssegers\Rollbar;
<?php

namespace Jenssegers\Rollbar;

use Illuminate\Support\ServiceProvider;
use InvalidArgumentException;
use Rollbar;
use RollbarNotifier;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Support\ServiceProvider;
use Jenssegers\Rollbar\RollbarLogHandler;

class RollbarServiceProvider extends ServiceProvider
{
Expand All @@ -15,43 +19,37 @@ class RollbarServiceProvider extends ServiceProvider
protected $defer = false;

/**
* Bootstrap the application events.
* Register the service provider.
*/
public function boot()
public function register()
{
$app = $this->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 = [
Expand All @@ -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);
});
}
}
}
10 changes: 10 additions & 0 deletions tests/RollbarLumenTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

abstract class RollbarLumenTest extends Orchestra\Testbench\TestCase
{
/**
*
* Due to the fact orchestra/testbench does not support testing Lumen bootstrapping, tests are yet to be written.
*
*/
}