Skip to content

add lumen support #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
*.iml
56 changes: 41 additions & 15 deletions src/RollbarServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<?php namespace Jenssegers\Rollbar;

use Illuminate\Foundation\Application as LaravelApplication;
use Laravel\Lumen\Application as LumenApplication;
use Illuminate\Support\ServiceProvider;
use InvalidArgumentException;
use Rollbar;
use RollbarNotifier;

//use Monolog\Handler\RollbarHandler;

class RollbarServiceProvider extends ServiceProvider
{
/**
Expand All @@ -20,35 +24,48 @@ class RollbarServiceProvider extends ServiceProvider
public function boot()
{
$app = $this->app;
if ($this->app instanceof LaravelApplication) {
// Listen to log messages.
$app['log']->listen(function ($level, $message, $context) use ($app) {
$app['Jenssegers\Rollbar\RollbarLogHandler']->log($level, $message, $context);
});
} elseif ($this->app instanceof LumenApplication) {
// Listen to log messages.
$app['log']->pushHandler(
app('Jenssegers\Rollbar\RollbarLogHandler', array(
$this->app['Rollbar']
))
);
}

// 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.
*/
public function register()
{
if ($this->app instanceof LumenApplication) {
$this->app->configure('services');
}

// Don't register rollbar if it is not configured.
if (! getenv('ROLLBAR_TOKEN') and ! $this->app['config']->get('services.rollbar')) {
if (!getenv('ROLLBAR_TOKEN') and !$this->app['config']->get('services.rollbar')) {
return;
}

$app = $this->app;

$this->app['RollbarNotifier'] = $this->app->share(function ($app) {
// Default configuration.
$defaults = [
'environment' => $app->environment(),
'root' => base_path(),
];
$defaults = array(
'environment' => $app->environment(),
'root' => base_path(),
);

$config = array_merge($defaults, $app['config']->get('services.rollbar', []));
$config = array_merge($defaults, $app['config']->get('services.rollbar', array()));

$config['access_token'] = getenv('ROLLBAR_TOKEN') ?: $app['config']->get('services.rollbar.access_token');
$config['access_token'] = getenv('ROLLBAR_TOKEN') ? getenv('ROLLBAR_TOKEN') : $app['config']->get('services.rollbar.access_token');

if (empty($config['access_token'])) {
throw new InvalidArgumentException('Rollbar access token not configured');
Expand All @@ -59,11 +76,20 @@ public function register()
return $rollbar;
});

$this->app['Jenssegers\Rollbar\RollbarLogHandler'] = $this->app->share(function ($app) {
$level = getenv('ROLLBAR_LEVEL') ?: $app['config']->get('services.rollbar.level', 'debug');
if ($this->app instanceof LaravelApplication) {
$this->app['Jenssegers\Rollbar\RollbarLogHandler'] = $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'], $app, $level);
});
} elseif ($this->app instanceof LumenApplication) {
$app['Jenssegers\Rollbar\RollbarLogHandler'] = $app->share(function ($app) {
$level = getenv('ROLLBAR_LEVEL') ?: $app['config']->get('services.rollbar.level', 'debug');

$handler = app('Monolog\Handler\RollbarHandler', [$this->app['RollbarNotifier'], $level]);
return $handler;
});
}

// Register the fatal error handler.
register_shutdown_function(function () use ($app) {
Expand Down