twotwentyseven / laravel-airbrake

Laravel 的 Airbrake 服务提供者 https://github.com/airbrake/phpbrake

0.5 2019-09-17 08:43 UTC

This package is not auto-updated.

Last update: 2024-09-26 08:56:18 UTC


README

这是一个为最新版本的 Airbrake PHP 包(https://github.com/airbrake/phpbrake)提供的 Laravel 服务提供者。

它是基于 https://github.com/TheoKouzelis/laravel-airbrake 的修改分支,以支持 Laravel 6.0。感谢 Theo Kouzelis 所做的艰苦工作。

服务提供者将使用 ID、密钥和环境名称配置一个 Airbrake\Notifier 实例。

安装

通过 composer 安装。

composer require twotwentyseven/laravel-airbrake

发布并填写 config/airbrake.php 文件,包含您的 ID 和密钥。

php artisan vendor:publish --provider="Twotwentyseven\LaravelAirbrake\ServiceProvider"

配置

projectId 和 projectKey 变量是必需的。其余的留空以使用 Airbrake 的默认值。

    'projectId'     => '',
    'projectKey'    => '',
    'environment'   => env('APP_ENV', 'production'),

    //leave the following options empty to use defaults

    'appVersion'    => '',
    'host'          => '',
    'rootDirectory' => '',
    'httpClient'    => '',

基本用法

>=5.6 自定义通道

将自定义的 "airbrake" 通道(如下所示)添加到 config/logging.php。然后将 "airbrake" 通道添加到栈通道。

//config/logging.php

    'channels' => [
        'stack' => [
            'driver' => 'stack',
            'channels' => ['single', 'airbrake'],
        ],

        'airbrake' => [
            'driver' => 'custom',
            'via' => Twotwentyseven\LaravelAirbrake\AirbrakeLogger::class,
            'level' => 'error',
        ],
    ]

异常处理器

如以下代码片段所示,通过 Laravel 异常处理器通知 airbrake。注入或创建一个新的 Airbrake\Notifier 对象实例,然后将异常传递给 notify 函数。

//app/Exceptions/Handler.php

/**
 * Report or log an exception.
 *
 * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
 *
 * @param  \Exception  $exception
 * @return void
 */
public function report(Exception $exception)
{
    if ($this->shouldReport($exception)) {
        $airbrakeNotifier = \App::make('Airbrake\Notifier');
        $airbrakeNotifier->notify($exception);
    }

    parent::report($exception);
}