Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sentry Laravel with different IoC namespace
<?php
namespace App\Spport;
use Illuminate\Support\Facades\Facade;
class SentryFacade extends Facade
{
protected static function getFacadeAccessor()
{
return 'sentry-laravel';
}
}
<?php
namespace App\Support;
use Log;
use Sentry\SentryLaravel\SentryLaravel;
use Illuminate\Support\ServiceProvider;
class SentryLaravelServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
$app = $this->app;
// Laravel 4.x compatibility
if (version_compare($app::VERSION, '5.0') < 0) {
$this->package('sentry/sentry-laravel', 'sentry-laravel');
$app->error(function (\Exception $e) use ($app) {
$app['sentry']->captureException($e);
});
$app->fatal(function ($e) use ($app) {
$app['sentry']->captureException($e);
});
$this->bindLogger($app['sentry-laravel']);
} else {
// the default configuration file
$this->publishes(array(
__DIR__ . '/config.php' => config_path('sentry-laravel.php'),
), 'config');
$this->bindLogger(app('sentry-laravel'));
}
}
public function bindLogger($client)
{
$handler = new \Raven_Breadcrumbs_MonologHandler($client);
$logger = Log::getMonolog();
$logger->pushHandler($handler);
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->singleton('sentry-laravel', function ($app) {
// sentry::config is Laravel 4.x
$user_config = $app['config']['sentry-laravel'] ?: $app['config']['sentry-laravel::config'];
// Make sure we don't crash when we did not publish the config file
if (is_null($user_config)) {
$user_config = [];
}
$client = SentryLaravel::getClient(array_merge(array(
'environment' => $app->environment(),
'prefixes' => array(base_path()),
'app_path' => app_path(),
), $user_config));
// bind user context if available
try {
if ($app['auth']->check()) {
$user = $app['auth']->user();
$client->user_context(array(
'id' => $user->getAuthIdentifier(),
));
}
} catch (\Exception $e) {
}
return $client;
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return array('sentry-laravel');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment