2

因此,目前 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 一起使用的一种很好的社区方式!

4

1 回答 1

2

我在面对同样的问题时遇到了这个问题。由于没有答案,我决定自己试一试。

我用我的解决方案写了一篇关于它的文章。

http://troccoli.it/rollbar-in-lumen/

简而言之,得到rollbar

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');
于 2018-12-08T09:34:53.917 回答