dragon-code / notify-exceptions
通过各种沟通渠道通知网站管理员任何错误。
Requires
- php: ^7.2.5|^8.0
- guzzlehttp/guzzle: ^7.0
- illuminate/bus: ^7.0|^8.0|^9.0
- illuminate/console: ^7.0|^8.0|^9.0
- illuminate/contracts: ^7.0|^8.0|^9.0
- illuminate/notifications: ^7.0|^8.0|^9.0
- illuminate/queue: ^7.0|^8.0|^9.0
- illuminate/support: ^7.0|^8.0|^9.0
- illuminate/view: ^7.0|^8.0|^9.0
- jaybizzle/crawler-detect: ^1.2
- symfony/debug: ^4.0
- symfony/http-kernel: ^4.0|^5.0|^6.0
Requires (Dev)
Suggests
- laravel/slack-notification-channel: Slack Notification Channel for Laravel
- lesstif/php-jira-rest-client: PHP classes interact Jira with the REST API.
- symfony/thanks: Give thanks (in the form of a GitHub) to your fellow PHP package maintainers
Conflicts
README
当然,您也可以自己创建。
安装
要获取最新版本的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
升级
- 在
composer.json
文件中将"andrey-helldar/notify-exceptions": "^3.0"
替换为"dragon-code/notify-exceptions": "^4.0"
; - 将
Helldar\Notifex
命名空间前缀替换为DragonCode\Notifex
; - 调用控制台命令
composer update
。
配置
忽略机器人
默认情况下,该包不会对在搜索机器人过程中创建的错误做出响应。
要启用来自机器人的错误消息,请更改config/notifex.php文件中的设置ignore_bots
。
默认值为false。
电子邮件
见配置文件。
示例电子邮件消息
Jira
如果您需要在Jira中创建问题,则需要安装包lesstif/php-jira-rest-client
composer require lesstif/php-jira-rest-client
Slack
如果您需要向Slack频道发送消息,则需要安装包laravel/slack-notification-channel
composer require laravel/slack-notification-channel
您的通知服务
您可以轻松连接您的通知服务。要做到这一点,在文件config/notifex.php
的jobs
块中添加对其作业的调用
\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
类,并被Notifex捕获。捕获的异常将立即出现在您配置的电子邮件中。
支持
该软件包默认支持将通知发送到以下服务
- 电子邮件 (默认,已启用)
- Slack (默认,未启用)
- Jira (默认,未启用)
许可
此软件包受MIT许可的许可。