andrey-helldar/notify-exceptions

此包已被弃用且不再维护。作者建议使用 dragon-code/notify-exceptions 包。

通过各种通讯渠道通知网站管理员任何错误。

v4.1.0 2022-01-17 16:00 UTC

README

Laravel Notify Exceptions

Total Downloads Latest Stable Version Latest Unstable Version License

您可以使用 Laravel 团队的官方插件和来自社区 插件

当然,您也可以自己创建。

安装

要获取 Notifex 的最新版本,只需使用 Composer 依赖项目。

composer require dragon-code/notify-exceptions

您当然可以手动更新 require 块并运行 composer update

{
    "require": {
        "dragon-code/notify-exceptions": "^4.0"
    }
}

您还可以发布配置文件以更改实现(即特定类到接口)。

php artisan vendor:publish --provider="DragonCode\Notifex\ServiceProvider"

然后从控制台调用 php artisan migrate 命令。

现在您可以使用 app('notifex') 方法。

andrey-helldar/notify-exceptions 升级

  1. composer.json 文件中将 "andrey-helldar/notify-exceptions": "^3.0" 替换为 "dragon-code/notify-exceptions": "^4.0"
  2. Helldar\Notifex 命名空间前缀替换为 DragonCode\Notifex
  3. 调用控制台的 composer update 命令。

配置

忽略机器人

默认情况下,该包不会对搜索机器人过程中创建的错误做出响应。

要启用来自机器人的错误消息,请更改 config/notifex.php 文件中的 ignore_bots 设置。

默认值为 false。

电子邮件

请参阅配置文件。

示例电子邮件消息

email-message

Jira

如果您需要在 Jira 中创建问题,则需要安装 lesstif/php-jira-rest-client 包。

composer require lesstif/php-jira-rest-client

2018-10-10_23-32-57

Slack

如果您需要向 Slack 频道发送消息,则需要安装 laravel/slack-notification-channel 包。

composer require laravel/slack-notification-channel

您的通知服务

您可以轻松连接您的通知服务。要做到这一点,在文件 config/notifex.phpjobs 块中添加对其作业的调用。

\DragonCode\Notifex\Jobs\ExampleJob::class

如果您需要向作业传递任何参数,您可以使用关联条目,其中键是作业类的链接,值是参数。

\DragonCode\Notifex\Jobs\ExampleJob::class => [
    'host'      => env('EXAMPLE_HOST'), // http://127.0.0.1:8080
    'user'      => env('EXAMPLE_USER'), // 'foo'
    'password'  => env('EXAMPLE_PASS'), // 'bar'
    'other_key' => env('EXAMPLE_OTHER_KEY'), // 12345
],

您的任务类应继承自抽象类 DragonCode\Notifex\Abstracts\JobAbstract。这将有助于正确创建工作类。

要获取设置值,您需要使用方法 getConfig($class, $key)

$host      = $this->getConfig(get_class(), 'host');
$user      = $this->getConfig(get_class(), 'user');
$password  = $this->getConfig(get_class(), 'password');
$other_key = $this->getConfig(get_class(), 'other_key');

// or add `config(string $key)` method:

private function config(string $key)
{
    return $this->getConfig(get_class(), $key);
}

$host      = $this->config('host');
$user      = $this->config('user');
$password  = $this->config('password');
$other_key = $this->config('other_key');

已完成类的示例可以在此处找到

值得注意的是,Laravel的标准任务用于调用

php artisan make:job <name>

它们应该移除调用接口 ShouldQueue 并扩展该类

// before
use Illuminate\Contracts\Queue\ShouldQueue;

class ExampleJob implements ShouldQueue {}

// after
use DragonCode\Notifex\Abstracts\JobAbstract;

class ExampleJob extends JobAbstract {}

因为抽象类包含了所有必要的类和接口的调用。

就这样!享受吧 😊

使用

将异常捕获添加到 app/Exceptions/Handler.php

public function report(Throwable $exception)
{
    parent::report($exception);
    
    if (app()->bound('notifex') && $this->shouldReport($exception)) {
        app('notifex')->send($exception);
    }
}

或者直接在您的代码中使用

try {
    $foo = $bar
} catch(\Exception $exception) {
    app('notifex')->send($exception);
}

重要!

为了实现将对象保存到数据库表中的可能性,该对象在序列化之前会进行处理。由于PHP中对象链接的特殊性,序列化不支持Throwable接口,因此,如果在处理变量之前调用 app('notifex')->send($exception),应用程序可能会引发错误 Expected array for frame 0

为了避免这种情况,请在发送通知之前严格使用 parent::report($exception)

测试消息

为了验证Notifex配置正确且我们的集成正常工作,请使用 notifex:test 命令

php artisan notifex:test

将抛出并捕获 DragonCode\Notifex\Exceptions\NotifexTestException 类。捕获到的异常将立即出现在您配置的电子邮件中。

支持

此包默认支持以下服务发送通知

  • 电子邮件 (默认,启用)
  • Slack (默认,禁用)
  • Jira (默认,禁用)

许可

此包受MIT许可证许可。