在 Lumen 5.7 中使用 Rollbar
Using Rollbar with Lumen 5.7
因此,目前 Lumen(不是 Laravel)的两个最流行的(恕我直言)rollbar 包是:
鉴于 https://github.com/jenssegers/laravel-rollbar
明确声明尝试为 5.x 添加 Lumen 支持,并且鉴于 James Elliot 在 adding Rollbar to 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) 反射异常
Class Illuminate\Foundation\Application 不存在
在 Container.php 行 838
在ReflectionParameter->getClass()
在 Container.php 行 838
在容器->resolveDependencies(数组(对象(反射参数),对象(反射参数),对象(反射参数)))
在 Container.php 行 807
在容器->构建('Jenssegers\Rollbar\RollbarLogHandler')
在 Container.php 行 658
在 Container->resolve('Jenssegers\Rollbar\RollbarLogHandler', array(object(Rollbar)))
在 Container.php 行 609
在 Container->make('Jenssegers\Rollbar\RollbarLogHandler', array(object(Rollbar)))
在 Application.php 第 260 行
在 Application->make('Jenssegers\Rollbar\RollbarLogHandler', array(object(Rollbar)))
在 Container.php 行 597
在容器->makeWith('Jenssegers\Rollbar\RollbarLogHandler', array(object(Rollbar)))
在 RollbarLumenServiceProvider.php 第 104 行
在 RollbarLumenServiceProvider->boot()
在 call_user_func_array(数组(对象(RollbarLumenServiceProvider),'boot'),数组())
在 BoundMethod.php 第 29 行
在 BoundMethod::Illuminate\Container{闭包}()
在 BoundMethod.php 第 87 行
at BoundMethod::callBoundMethod(object(Application), array(object(RollbarLumenServiceProvider), 'boot'), object(Closure))
在 BoundMethod.php 第 31 行
在BoundMethod::call(对象(应用程序),数组(对象(RollbarLumenServiceProvider),'boot'),数组(),空)
在 Container.php 第 572 行
在容器->调用(数组(对象(RollbarLumenServiceProvider),'boot'))
在 Application.php 第 237 行
在应用程序->bootProvider(对象(RollbarLumenServiceProvider))
在 Application.php 行 222
在 Application->Laravel\Lumen{closure}(object(RollbarLumenServiceProvider), 'App\Providers\RollbarLumenServiceProvider')
正是在这一点上,我卡住了。有谁知道如何解决这个错误?我不是服务容器或 rollbar wiz,将不胜感激任何帮助。希望这将成为让 Rollbar 与 Lumen 5.7 一起工作的一个很好的社区方式!
我在遇到同样的问题时遇到了这个问题。由于没有答案,我决定自己试一试。
我用我的解决方案写了一篇关于它的文章。
http://troccoli.it/rollbar-in-lumen/
简而言之,获取滚动条
composer require rollbar/rollbar
将 rollbar
频道添加到您的 config/logging.php
<?php
return [
'default' => env('LOG_CHANNEL', 'stack'),
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['rollbar'],
],
'rollbar' => [
'driver' => 'monolog',
'handler' => Rollbar\Monolog\Handler\RollbarHandler::class,
'access_token' => env('ROLLBAR_ACCESS_TOKEN'),
'level' => 'debug',
],
],
];
写一个服务提供者RollbarServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Support\ServiceProvider;
use Rollbar\RollbarLogger;
use Rollbar\Rollbar;
class RollbarServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton(RollbarLogger::class, function () {
$config = $this->app->make(Repository::class);
$defaults = [
'environment' => app()->environment(),
'root' => base_path(),
'handle_exception' => true,
'handle_error' => true,
'handle_fatal' => true,
];
$rollbarConfig = array_merge($defaults, $config->get('logging.channels.rollbar', []));
$handleException = (bool)array_pull($rollbarConfig, 'handle_exception');
$handleError = (bool)array_pull($rollbarConfig, 'handle_error');
$handleFatal = (bool)array_pull($rollbarConfig, 'handle_fatal');
Rollbar::init($rollbarConfig, $handleException, $handleError, $handleFatal);
return Rollbar::logger();
});
}
}
添加 post_server_item- token (you can get this from your rollbar account) into the
.env` 文件
ROLLBAR_ACCESS_TOKEN=ROLLBAR_POST_SERVER_ITEM_TOKEN
最后 link 所有这些都在 bootstrap/app.php
$app->register(\App\Providers\RollbarServiceProvider::class);
$app->configure('logging');
因此,目前 Lumen(不是 Laravel)的两个最流行的(恕我直言)rollbar 包是:
鉴于 https://github.com/jenssegers/laravel-rollbar
明确声明尝试为 5.x 添加 Lumen 支持,并且鉴于 James Elliot 在 adding Rollbar to 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) 反射异常 Class Illuminate\Foundation\Application 不存在 在 Container.php 行 838
在ReflectionParameter->getClass() 在 Container.php 行 838
在容器->resolveDependencies(数组(对象(反射参数),对象(反射参数),对象(反射参数))) 在 Container.php 行 807
在容器->构建('Jenssegers\Rollbar\RollbarLogHandler') 在 Container.php 行 658
在 Container->resolve('Jenssegers\Rollbar\RollbarLogHandler', array(object(Rollbar))) 在 Container.php 行 609
在 Container->make('Jenssegers\Rollbar\RollbarLogHandler', array(object(Rollbar))) 在 Application.php 第 260 行
在 Application->make('Jenssegers\Rollbar\RollbarLogHandler', array(object(Rollbar))) 在 Container.php 行 597
在容器->makeWith('Jenssegers\Rollbar\RollbarLogHandler', array(object(Rollbar))) 在 RollbarLumenServiceProvider.php 第 104 行
在 RollbarLumenServiceProvider->boot() 在 call_user_func_array(数组(对象(RollbarLumenServiceProvider),'boot'),数组()) 在 BoundMethod.php 第 29 行
在 BoundMethod::Illuminate\Container{闭包}() 在 BoundMethod.php 第 87 行
at BoundMethod::callBoundMethod(object(Application), array(object(RollbarLumenServiceProvider), 'boot'), object(Closure)) 在 BoundMethod.php 第 31 行
在BoundMethod::call(对象(应用程序),数组(对象(RollbarLumenServiceProvider),'boot'),数组(),空) 在 Container.php 第 572 行
在容器->调用(数组(对象(RollbarLumenServiceProvider),'boot')) 在 Application.php 第 237 行
在应用程序->bootProvider(对象(RollbarLumenServiceProvider)) 在 Application.php 行 222
在 Application->Laravel\Lumen{closure}(object(RollbarLumenServiceProvider), 'App\Providers\RollbarLumenServiceProvider')
正是在这一点上,我卡住了。有谁知道如何解决这个错误?我不是服务容器或 rollbar wiz,将不胜感激任何帮助。希望这将成为让 Rollbar 与 Lumen 5.7 一起工作的一个很好的社区方式!
我在遇到同样的问题时遇到了这个问题。由于没有答案,我决定自己试一试。
我用我的解决方案写了一篇关于它的文章。
http://troccoli.it/rollbar-in-lumen/
简而言之,获取滚动条
composer require rollbar/rollbar
将 rollbar
频道添加到您的 config/logging.php
<?php
return [
'default' => env('LOG_CHANNEL', 'stack'),
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['rollbar'],
],
'rollbar' => [
'driver' => 'monolog',
'handler' => Rollbar\Monolog\Handler\RollbarHandler::class,
'access_token' => env('ROLLBAR_ACCESS_TOKEN'),
'level' => 'debug',
],
],
];
写一个服务提供者RollbarServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Support\ServiceProvider;
use Rollbar\RollbarLogger;
use Rollbar\Rollbar;
class RollbarServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton(RollbarLogger::class, function () {
$config = $this->app->make(Repository::class);
$defaults = [
'environment' => app()->environment(),
'root' => base_path(),
'handle_exception' => true,
'handle_error' => true,
'handle_fatal' => true,
];
$rollbarConfig = array_merge($defaults, $config->get('logging.channels.rollbar', []));
$handleException = (bool)array_pull($rollbarConfig, 'handle_exception');
$handleError = (bool)array_pull($rollbarConfig, 'handle_error');
$handleFatal = (bool)array_pull($rollbarConfig, 'handle_fatal');
Rollbar::init($rollbarConfig, $handleException, $handleError, $handleFatal);
return Rollbar::logger();
});
}
}
添加 post_server_item- token (you can get this from your rollbar account) into the
.env` 文件
ROLLBAR_ACCESS_TOKEN=ROLLBAR_POST_SERVER_ITEM_TOKEN
最后 link 所有这些都在 bootstrap/app.php
$app->register(\App\Providers\RollbarServiceProvider::class);
$app->configure('logging');