Home > Article > PHP Framework > How to use Slack for exception notification in Laravel
Exception handling is an unavoidable problem in the software development process. For a program with well-designed code and efficient code, the possibility of exceptions will be relatively low, but this does not mean that exceptions will not occur. Some exceptions may even cause serious consequences, so how to discover and handle exceptions in the program in a timely manner It becomes very important.
Recommended: laravel tutorial
Usually, we can rely on user feedback and often check program logs to find problems with the program. But this is either unreliable or not timely, so below we introduce a more effective method-using exception information run by the Slack notification program.
Slack is an instant messaging software, similar to QQ. It provides an open API, which can be called to send messages to designated individuals or channels in your team, so using it for exception notification is Couldn't be more appropriate.
1. Install the maknz/slack-laravel package
具体的安装方法请参考 Github 上的 [readme](https://github.com/maknz/slack-laravel)。
2. Configure
安装完成后使用 php artisan vendor:publish 生成 config\slack.php 配置文件,然后在.env文件中添加如下三项配置值. ``` SLACK_ENDPOINT=//slack 终端,即 slack 接口地址 SLACK_CHANNEL=//消息默认接收频道 SLACK_USERNAME//消息默认接收人 ``` > 当然,也可以直接直接在 config\slack.php 对应配置的默认值而不使用 .env,但并不推荐这样做。 > 根据实际需要设置 SLACK_CHANNEL 和 SLACK_USERNAME ,二者并不是必须的
3. Adjust the report method of the AppExceptionsHandler class to implement the logic of Slack notification of exception information. The code is as follows:
/** * Report or log an exception. * * This is a great spot to send exceptions to Sentry, Bugsnag, etc. * * @param \Exception $e * @return void */ public function report(Exception $e) { if ($this->shouldReport($e)) { $slackMessage = "\n[Error.{$e->getCode()}] {$e->getMessage()}"; $slackMessage.= "\n[Line.{$e->getLine()}] {$e->getFile()}"; $slackMessage.= "\n[Time] ".date('Y-m-d H:i:s'); try { Slack::to(config('slack.channel'))->send($slackMessage); } catch (\Exception $eOther) { \Log::info($slackMessage); } } return parent::report($e); }
For some exceptions that may occur frequently but are not fatal, such as 404 NotFoundHttpException, we may not want the program to notify it. To do this, we only need to add the specified exception type to the $dontReort attribute in the Aop\Exceptions\Handler class.
protected $dontReport = [ NotFoundHttpException::class, // ... ];
The example shows sending information to a specified channel (channel). Of course, you can also send it to a specified person, or other more complex usages. However, please note that the specified user name and channel already exist in your slack team, otherwise an error will occur.
In this way, when an exception occurs in the program, the operation and maintenance can receive the relevant notification information as soon as possible, which facilitates timely processing.
Related recommendations, PHP video tutorial learning address: https://www.php.cn/course/list/29/type/2.html
The above is the detailed content of How to use Slack for exception notification in Laravel. For more information, please follow other related articles on the PHP Chinese website!