scsuoft/laravel-exception-notification

此包已 废弃 并不再维护。未建议替代包。

异常通知。

2.4.2 2018-01-19 17:33 UTC

This package is not auto-updated.

Last update: 2020-07-10 23:52:07 UTC


README

Software License

此包会在您的生产应用中抛出异常时通知您。

安装

composer require scsuoft/laravel-exception-notification

接下来,您需要在 config/app.php 中注册 Service Provider

$providers = [
    ...
    Razorpay\Slack\Laravel\ServiceProviderLaravel5::class,
    
    LaravelExceptionNotification\ExceptionNotificationServiceProvider::class,
    ...
];
$facades = [
    ...
    'Slack' => Razorpay\Slack\Laravel\Facade::class,
    ...
];

然后发布配置文件

php artisan vendor:publish --provider="Razorpay\Slack\Laravel\ServiceProviderLaravel5"

php artisan vendor:publish --provider="LaravelExceptionNotification\ExceptionNotificationServiceProvider"

配置

首先,您必须配置 config/slack.php

变量 'endpoint' 需要定义为您的 Slack 面板中的 webhook。例如: 'https://hooks.slack.com/services/xxxxx'

config/exception-notification.php 的内容基本自解释,下面的 Slack 数组将覆盖 config/slack.php

在 .env 中设置环境变量 EXCEPTION_EMAIL 以您的警报电子邮件地址。同时,您还需要在 .env 中配置与电子邮件相关的设置

<?php

return [
    /*
     |--------------------------------------------------------------------------
     | Enabled sender drivers
     |--------------------------------------------------------------------------
     |
     | Send a notification about exception in your application to supported channels.
     |
     | Supported: "mail", "slack". You can use multiple drivers.
     |
     */
    'drivers'      => [ 'mail', 'slack' ],

    /*
     |--------------------------------------------------------------------------
     | Enabled application environments
     |--------------------------------------------------------------------------
     |
     | Set environments that should generate notifications.
     |
     */
    'environments' => [ 'production' ],

    /*
     |--------------------------------------------------------------------------
     | Mail Configuration
     |--------------------------------------------------------------------------
     |
     | It uses your app default Mail driver. You shouldn't probably touch the view
     | property unless you know what you're doing.
     |
     */
    'mail'         => [
        'from' => 'sender@example.com',
        'to'   => env('EXCEPTION_EMAIL')
    ],

    /*
     * Uses maknz\slack package.
     */
    'slack'        => [
        'channel'  => '#bugtracker',//needs to be exact channel name
        'username' => 'Exception Notification',//something you just made up
        'icon'     => ':robot_face:',
    ],
];

使用方法

要开始捕获异常,您有两个选择。

第一个选项:从包提供的异常处理器扩展(app/Exceptions/Handler.php

use LaravelExceptionNotification\NotificationExceptionHandler;
...
class Handler extends NotificationExceptionHandler

第二个选项:将 app/Exceptions/Handler.php 中的 report 方法修改如下

public function report(Exception $exception)
{
    foreach ($this->dontReport as $type) {
        if ($exception instanceof $type) {
            return parent::report($exception);
        }
    }

    if (app()->bound('exception-notification')) {
        app('exception-notification')->notifyException($exception);
    }

    return parent::report($exception);
}

许可

此库采用 MIT 许可证。请参阅 许可证文件 了解更多信息。