captbrogers/laravel-rollbar

此包已被废弃且不再维护。作者建议使用 rollbar/rollbar-laravel 包。

Laravel 5.4 项目的 Rollbar 错误监控集成

v2.0.2 2017-07-10 17:22 UTC

README

Build Status Coverage Status

为 Laravel 项目提供 Rollbar 错误监控集成。此库向 Laravel 的日志组件添加监听器。Laravel 的会话信息将被发送到 Rollbar,以及一些其他有用的信息,如 '环境'、'服务器' 和 '会话'。

rollbar

安装

使用 composer 安装

composer require captbrogers/rollbar

将服务提供者添加到 config/app.php 中的 'providers' 数组

Captbrogers\Rollbar\RollbarServiceProvider::class,

如果您只想为特定环境启用 Rollbar 报告,您可以在 AppServiceProvider 中有条件地加载服务提供者

    public function register()
    {
        if ($this->app->environment('production')) {
            $this->app->register(\Captbrogers\Rollbar\RollbarServiceProvider::class);
        }
    }

配置

此包通过位于 config/services.php 的服务配置文件进行配置。所有配置变量将直接传递给 Rollbar

'rollbar' => [
    'access_token' => env('ROLLBAR_TOKEN'),
    'level' => env('ROLLBAR_LEVEL'),
],

level 变量定义了将日志消息发送到 Rollbar 的最低日志级别。对于开发,您可以将其设置为 debug 以发送所有日志消息,或设置为 none 以不发送任何消息。对于生产,您可以将其设置为 error 以忽略所有信息和调试消息。

使用方法

要自动监控异常,只需在 app/Exceptions/Handler.php 中的错误处理器中使用 Log 门面即可

public function report(Exception $exception)
{
    \Log::error($exception); //rollbar
    parent::report($exception);
}

对于 Laravel 4 安装,此文件位于 app/start/global.php

App::error(function(Exception $exception, $code)
{
    Log::error($exception);
});

您的其他日志消息也将被发送到 Rollbar

\Log::debug('Here is some debug information');

注意:致命异常始终会被发送到 Rollbar。

上下文信息

您可以将用户信息作为上下文传递,如下所示

\Log::error('Something went wrong', [
    'person' => ['id' => 123, 'username' => 'John Doe', 'email' => 'john@doe.com']
]);

或传递一些额外信息

\Log::warning('Something went wrong', [
    'download_size' => 3432425235
]);