nqxcode / laravel-exception-notifier
通过telegram和email通知laravel项目中的异常
v1.0.12
2020-11-12 11:30 UTC
Requires
- php: ^7.4
- ext-json: *
- ext-phar: *
- facade/ignition: ^2.0
- laravel-notification-channels/telegram: ^0.5.0
- laravel/framework: ^8.0
Requires (Dev)
- orchestra/testbench: ^6.0
Suggests
- ext-posix: Needed for getting the process info, like user name.
README
通过 telegram 和 email 通知laravel项目中的异常。
通知附件包含使用 facade/ignition 渲染的 gzip压缩html 格式的异常信息,例如
安装
在composer.json中添加此包
composer require "nqxcode/laravel-exception-notifier"
配置
通过以下命令将配置文件发布到项目中
php artisan vendor:publish --provider="Nqxcode\LaravelExceptionNotifier\ServiceProvider"
修改默认配置文件 config/laravel-exception-notifier.php
,例如移除不必要的通道
<?php return [ 'routes' => [ [ 'channel' => 'mail', 'route' => env('EXCEPTION_NOTIFIER_EMAIL', 'example@gmail.com'), ], [ 'channel' => 'telegram', 'route' => env('EXCEPTION_NOTIFIER_TELEGRAM_USER_ID', '1234567890') ] ], 'subject' => 'Исключение на сайте '.env('APP_URL'), ];
将以下内容添加到 config/services.php
<?php return [ // ... 'telegram-bot-api' => [ 'token' => env('TELEGRAM_BOT_TOKEN'), ] ];
在 .env
中添加正确的环境变量
# For email notification EXCEPTION_NOTIFIER_EMAIL=test@test.com # For telegram notification # recipient EXCEPTION_NOTIFIER_TELEGRAM_USER_ID=423460522 # sender TELEGRAM_BOT_TOKEN=1160101879:AAFzuda0o7X6Dp4RBp00K-7dYjjnwMY887A
要获取 telegram机器人令牌,在 BotFather
中创建新的机器人,使用此操作 /newbot
,更多详情请见 这里。
要在 production
环境中通知异常,修改文件 app/Exceptions/Handler.php
中的 report
方法
<?php namespace App\Exceptions; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Nqxcode\LaravelExceptionNotifier\ExceptionNotifierInterface; use Throwable; class Handler extends ExceptionHandler { // ... public function report(Throwable $exception) { if ($this->container->isProduction() && $this->shouldReport($exception)) { $this->container->make(ExceptionNotifierInterface::class)->notify($exception); } parent::report($exception); } // ... }
许可证
该软件包许可在MIT许可证下。