wirebox/laravel-airbrake

Laravel服务提供程序,用于Airbrake https://github.com/airbrake/phpbrake

v1.0.3 2024-05-03 11:18 UTC

This package is auto-updated.

Last update: 2024-09-03 11:57:57 UTC


README

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

服务提供程序将配置Airbrake\Notifier实例的ID、密钥和环境名称。

安装

通过composer要求此包。

composer require wirebox/laravel-airbrake

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

<?php

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

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

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

配置

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

    'projectId'     => env('AIRBRAKE_ID', ''),
    'projectKey'    => env('AIRBRAKE_KEY', ''),
    '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' => Wirebox\LaravelAirbrake\AirbrakeLogger::class,
            'level' => 'error',
        ],
    ]

异常处理器

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

Laravel 11

更新app异常处理器的withExceptions方法


//bootstrap/app.php
->withExceptions(function (Exceptions $exceptions) {
        $exceptions->report(function (Throwable $th) { // The Exceptions collection will contain Throwable instances. Useful for reporting PHP fatal errors.
            if (App::environment('staging', 'production') ) {
                $airbrakeNotifier = App::make('Airbrake\Notifier');
                $airbrakeNotifier->notify($th);
            }
        });
    })...

//app/Exceptions/Handler.php

< Laravel 11

/**
 * Report or log an exception.
 *
 * @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 Wirebox\LaravelAirbrake\AirbrakeHandler($app))->handle();
    $monologHandler = new Airbrake\MonologHandler($airbrakeNotifier, Monolog\Logger::ERROR);
    $monolog->pushHandler($monologHandler);
});