因此,目前 Lumen(不是 Laravel)最流行的两个(恕我直言)rollbar 包是:
鉴于https://github.com/jenssegers/laravel-rollbar
明确声明尝试添加对 5.x 的 Lumen 支持,并且鉴于 James Elliot 有一篇精彩的关于将 Rollbar 添加到 Lumen 5.2的教程,我尝试更新他的教程的代码并将其用于 Lumen 5.7。
他的大部分作品都在他 RollbarLumenServiceProvider
的作品中,看起来像这样:
namespace App\Providers;
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('rollbar');
// Don't register rollbar if it is not configured.
if (! getenv('ROLLBAR_TOKEN') and ! $this->app['config']->get('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
];
}
}
我尝试为 Lumen 5.7 更新此内容以考虑弃用和破坏性更改,如下所示:
<?php
namespace App\Providers;
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;
private function getApp($app): \Laravel\Lumen\Application
{
return $app;
}
/**
* Register the service provider.
*/
public function register()
{
$app = $this->getApp($this->app);
$app->configure('rollbar');
// Don't register rollbar if it is not configured.
if (!getenv('ROLLBAR_TOKEN') and !$app['config']->get('rollbar')) {
return;
}
$app->singleton(RollbarNotifier::class, function (\Laravel\Lumen\Application $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->singleton(RollbarHandler::class, function (\Laravel\Lumen\Application $app)
{
$level = getenv('ROLLBAR_LEVEL') ?: $app['config']->get('services.rollbar.level', 'debug');
//$handler = app(RollbarHandler::class, [$app[RollbarNotifier::class], $level]);
$handler = $app->makeWith(RollbarHandler::class, [$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->makeWith(RollbarLogHandler::class, [$app[Rollbar::class]])
);
}
public function provides()
{
return [
RollbarLogHandler::class
];
}
}
我认为它几乎有效。我在此方法中遇到异常:
public function boot()
{
$app = $this->app;
// Listen to log messages.
$app['log']->pushHandler(
$app->makeWith(RollbarLogHandler::class, [$app[Rollbar::class]])
);
}
这是异常跟踪:
(1/1) ReflectionException Class Illuminate\Foundation\Application 在 Container.php 第 838 行中不存在
在 Container.php 第 838 行中的 ReflectionParameter->getClass()
在 Container.php 第 807 行中的 Container->resolveDependencies(array(object(ReflectionParameter), object(ReflectionParameter), object(ReflectionParameter)))
在 Container.php 第 658 行中的 Container->build('Jenssegers\Rollbar\RollbarLogHandler')
在 Container->resolve('Jenssegers\Rollbar\RollbarLogHandler', array(object(Rollbar))) 在 Container.php 行 609
在 Application.php 第 260 行中的 Container->make('Jenssegers\Rollbar\RollbarLogHandler', array(object(Rollbar)))
在 Container.php 第 597 行中的 Application->make('Jenssegers\Rollbar\RollbarLogHandler', array(object(Rollbar)))
在 Container->makeWith('Jenssegers\Rollbar\RollbarLogHandler', array(object(Rollbar))) 在 RollbarLumenServiceProvider.php 第 104 行
在 RollbarLumenServiceProvider->boot() at call_user_func_array(array(object(RollbarLumenServiceProvider), 'boot'), array()) 在 BoundMethod.php 第 29 行
在 BoundMethod.php 第 87 行中的 BoundMethod::Illuminate\Container{closure}()
在 BoundMethod::callBoundMethod(object(Application), array(object(RollbarLumenServiceProvider), 'boot'), object(Closure)) 在 BoundMethod.php 第 31 行
在 Container.php 第 572 行中的 BoundMethod::call(object(Application), array(object(RollbarLumenServiceProvider), 'boot'), array(), null)
在 Application.php 第 237 行中的 Container->call(array(object(RollbarLumenServiceProvider), 'boot'))
在 Application.php 第 222 行中的 Application->bootProvider(object(RollbarLumenServiceProvider))
在 Application->Laravel\Lumen{closure}(object(RollbarLumenServiceProvider), 'App\Providers\RollbarLumenServiceProvider')
正是在这一点上,我被卡住了。有谁知道如何解决这个错误?我不是服务容器或防滚杆专家,我将不胜感激。希望这将成为让 Rollbar 与 Lumen 5.7 一起使用的一种很好的社区方式!