kouz/laravel-airbrake

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

0.8.0 2022-05-19 13:09 UTC

This package is auto-updated.

Last update: 2024-09-20 00:31:02 UTC


README

这是一个为最新 Airbrake PHP 包 https://github.com/airbrake/phpbrake 提供的 Laravel 服务提供器

服务提供器将配置一个 Airbrake\Notifier 实例,包含 ID、密钥和环境名称。

安装

通过 composer 需求此包。

composer require kouz/laravel-airbrake

对于 Laravel >=5.5,此包将被发现。对于 Laravel <=5.4,请将包添加到 config/app.php 中的服务提供器列表。

<?php

    //config/app.php
  
    'providers' => [
        Kouz\LaravelAirbrake\ServiceProvider::class,
    ],

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

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

配置

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

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

    //leave the following options empty to use defaults

    'host'          => null, #airbrake api host e.g.: 'api.airbrake.io' or 'http://errbit.example.com
    'appVersion'    => null,
    'revision'      => null, #git revision
    'rootDirectory' => null,
    'keysBlacklist' => null, #list of keys containing sensitive information that must be filtered out
    'httpClient'    => null, #http client implementing GuzzleHttp\ClientInterface

基本用法

>=5.6 自定义通道

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

//config/logging.php

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

        'airbrake' => [
            'driver' => 'custom',
            'via' => Kouz\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) && App::environment(['production', 'staging'])) {
        $airbrakeNotifier = \App::make('Airbrake\Notifier');
        $airbrakeNotifier->notify($exception);
    }

    parent::report($exception);
}

<=5.5 自定义 Monolog 配置

要将它配置为 Monolog 处理器,您必须在 bootstrap/app.php 中创建一个自定义配置。此回调函数在服务提供器加载之前被调用。因此,必须直接使用我们的 AirbrakeHandler 类,而不是提供器。

//bootstrap/app.php

$app->configureMonologUsing(function($monolog) use ($app) {
    $airbrakeNotifier = (new Kouz\LaravelAirbrake\AirbrakeHandler($app))->handle();
    $monologHandler = new Airbrake\MonologHandler($airbrakeNotifier, Monolog\Logger::ERROR);
    $monolog->pushHandler($monologHandler);
});