将Rollbar与Lumen 5.7配合使用

时间:2018-11-21 20:17:11

标签: lumen rollbar

因此,当前用于Lumen(不是Laravel)的两个最受欢迎的(IMHO)防滚架套件是:

鉴于https://github.com/jenssegers/laravel-rollbar明确声明尝试添加对5.x的Lumen支持,并且考虑到James Elliot在adding Rollbar to Lumen 5.2上有一个精彩的教程,我尝试更新其教程的代码并使用它适用于流明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
        ];
    }
}

我尝试针对流明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
        ];
    }
}

我认为ALMOST可行。我在此方法中遇到异常:

    public function boot()
    {
        $app = $this->app;

        // Listen to log messages.
        $app['log']->pushHandler(
            $app->makeWith(RollbarLogHandler::class, [$app[Rollbar::class]])
        );
    }

这是“异常”跟踪:

(1/1)ReflectionException 类Illuminate \ Foundation \ Application不存在 在Container.php第838行中

在ReflectionParameter-> getClass() 在Container.php第838行中

在Container-> resolveDependencies(array(object(ReflectionParameter),object(ReflectionParameter),object(ReflectionParameter))) 在Container.php第807行中

在Container-> build('Jenssegers \ Rollbar \ RollbarLogHandler') 在Container.php第658行

at Container-> resolve('Jenssegers \ Rollbar \ RollbarLogHandler',array(object(Rollbar)))) 在Container.php第609行中

at Container-> make('Jenssegers \ Rollbar \ RollbarLogHandler',array(object(Rollbar))) 在Application.php第260行中

在Application-> make('Jenssegers \ Rollbar \ RollbarLogHandler',array(object(Rollbar))) 在Container.php第597行中

在Container-> makeWith('Jenssegers \ Rollbar \ RollbarLogHandler',array(object(Rollbar))) 在RollbarLumenServiceProvider.php第104行中

在RollbarLumenServiceProvider-> boot() 在call_user_func_array(array(object(RollbarLumenServiceProvider),'boot'),array()) 在BoundMethod.php第29行中

at BoundMethod :: Illuminate \ Container {closure}() 在BoundMethod.php第87行

在BoundMethod :: callBoundMethod(object(Application),array(object(RollbarLumenServiceProvider),'boot'),object(Closure)) 在BoundMethod.php第31行中

at BoundMethod :: call(object(Application),array(object(RollbarLumenServiceProvider),'boot'),array(),null) 在Container.php第572行

在Container->调用(array(object(RollbarLumenServiceProvider),'boot')) 在Application.php 237行中

在Application-> bootProvider(object(RollbarLumenServiceProvider)) 在Application.php第222行

在应用程序-> Laravel \ Lumen {closure}(对象(RollbarLumenServiceProvider,'App \ Providers \ RollbarLumenServiceProvider')中

在这一点上我被困住了。有人知道如何解决此错误吗?我不是服务容器或滚动条向导,将不胜感激。希望这将是让Rollbar与Lumen 5.7一起使用的一种很好的社区方式!

1 个答案:

答案 0 :(得分:1)

面对相同的问题时,我遇到了这个问题。既然没有答案,我决定自己去解决。

我写了一篇有关解决方案的文章。

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

最后在bootstrap/app.php

中将所有这些链接在一起
$app->register(\App\Providers\RollbarServiceProvider::class);
$app->configure('logging');