首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Lumen 5.7中使用Rollbar

在Lumen 5.7中使用Rollbar
EN

Stack Overflow用户
提问于 2018-11-21 20:17:11
回答 1查看 607关注 0票数 2

因此,目前用于Lumen (而不是Laravel)的两个最流行的(IMHO)滚动条包是:

考虑到https://github.com/jenssegers/laravel-rollbar明确声明试图添加对5.x的Lumen支持,并且考虑到James在向Lumen 5.2添加滚动条上有这个精彩的教程,我试图更新他的教程的代码,并将其用于Lumen5.7。

他的大部分工作都是在 his RollbarLumenServiceProvider中完成的,看起来如下所示:

代码语言:javascript
复制
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更新这一内容,考虑到弃用和破坏更改,如下所示:

代码语言:javascript
复制
<?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
        ];
    }
}

我觉得它几乎成功了。在这种方法中,我得到了一个例外:

代码语言:javascript
复制
    public function boot()
    {
        $app = $this->app;

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

下面是异常跟踪:

(1/1) ReflectionException类照明\Foundation\Application不存在于Container.php行838

在Container.php第838行中的反射参数->getClass()

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

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

在Container->resolve('Jenssegers\Rollbar\RollbarLogHandler',数组(object(Rollbar))上的Container.php第609行

在Container->make('Jenssegers\Rollbar\RollbarLogHandler',数组(object(Rollbar))上的Application.php第260行

在Application->make('Jenssegers\Rollbar\RollbarLogHandler',数组(object(Rollbar))上的Container.php第597行

在Container->makeWith('Jenssegers\Rollbar\RollbarLogHandler',数组(object(Rollbar))上的RollbarLumenServiceProvider.php行104

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

在BoundMethod.php第87行中::照明\容器{闭包}()

在BoundMethod::callBoundMethod(object(Application),数组(object(RollbarLumenServiceProvider),'boot'),object(闭包)处,BoundMethod.php第31行

在Container.php第572行中调用(对象(应用程序)、数组(对象(Container.php)、“引导”)、数组()、null)

(在Container->call(array(object(RollbarLumenServiceProvider),‘boot’)在Application.php第237行中

在Application->bootProvider(object(RollbarLumenServiceProvider))在Application.php 222号线

在Application->Laravel\Lumen{closure}(object(RollbarLumenServiceProvider),'App\Providers\RollbarLumenServiceProvider')

就在这个时候我被困住了。有人知道如何修复这个错误吗?我不是一个服务容器或滚动条wiz,并将感谢任何帮助。希望这将成为一个很好的社区方式,让Lumen5.7使用Rollbar!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-08 09:34:53

我在面对同样的问题时遇到了这个问题。由于没有人回答,我决定亲自去做。

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

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

简单地说,得到滚动条

代码语言:javascript
复制
composer require rollbar/rollbar

rollbar通道添加到config/logging.php

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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`文件

代码语言:javascript
复制
ROLLBAR_ACCESS_TOKEN=ROLLBAR_POST_SERVER_ITEM_TOKEN

最后,在bootstrap/app.php中将所有这些连接在一起

代码语言:javascript
复制
$app->register(\App\Providers\RollbarServiceProvider::class);
$app->configure('logging');
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53419870

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档