Skip to content

Commit

Permalink
Adds Lumen 5.x support
Browse files Browse the repository at this point in the history
  • Loading branch information
GiamPy5 committed May 23, 2016
1 parent 24d5b72 commit f02e64d
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 22 deletions.
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*
68 changes: 46 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -35,36 +49,46 @@ 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.

### Context informaton

You can pass user information as context like this:

\Log::error('Something went wrong', [
'person' => ['id' => 123, 'username' => 'John Doe', 'email' => '[email protected]']
]);
```php
\Log::error('Something went wrong', [
'person' => ['id' => 123, 'username' => 'John Doe', 'email' => '[email protected]']
]);
```

Or pass some extra information:

\Log::warning('Something went wrong', [
'download_size' => 3432425235
]);
```php
\Log::warning('Something went wrong', [
'download_size' => 3432425235
]);
```
97 changes: 97 additions & 0 deletions src/RollbarLumenServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php namespace Jenssegers\Rollbar;

use Jenssegers\Rollbar\RollbarLogHandler;
use Illuminate\Support\ServiceProvider;
use InvalidArgumentException;
use Monolog\Handler\RollbarHandler;
use Rollbar;
use RollbarNotifier;

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

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

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

0 comments on commit f02e64d

Please sign in to comment.