Exception handling is an unavoidable problem in software development. For a program with well-designed and efficient code, the possibility of anomalies is relatively low, but this does not mean that there will not be anomalies, and some anomalies may even cause serious consequences, so it is very important to find and deal with anomalies in the program in time.

In general, we can rely on user feedback and check the application logs frequently to find problems in the application. But this is either unreliable or not timely, so here’s a more efficient way to do it: Use Slack to notify exceptions that come up.

Slack, an instant-messaging app similar to QQ, offers an open API that can be called to send messages to designated individuals or channels on your team, making it perfect for exception notifications.

Install the Maknz/Slack-laravel package

Specific installation method please refer to the making of [the readme] (https://github.com/maknz/slack-laravel).

2. The configuration

After the installation, use PHP artisan Vendor :publish to generate the config\slack.php configuration file and add the following three configuration values to the. Env file: SLACK_ENDPOINT=// Slack terminal, The slack interface address SLACK_CHANNEL = / / message receiving channel SLACK_USERNAME / / by default the default recipient ` ` ` > of course, can also directly in the config \ slack PHP corresponding configuration default value instead of using the env, But this is not recommended. SLACK_CHANNEL and SLACK_USERNAME are not requiredCopy the code

3. Adjust the Report method of AppExceptionsHandler class to implement the Slack logic for notifies 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);

}Copy the code

For 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 simply add the specified exception type to the $dontReort attribute in the Aop\Exceptions\Handler class.

protected $dontReport= [ NotFoundHttpException::class, // ... ] ;Copy the code

The example shows sending messages to a specified channel, but you can also send messages to a specified person, or something more complicated. Note that the specified user name and channel already exist in your Slack team, otherwise you will get an error.

In this way, when exceptions occur in the program, o&M can receive relevant notification information in the first time, which is convenient for timely handling.

So that’s how to use Slack for exception notification in Laravel

To learn more, please visit:

How to become an architect from a coder


I hope the above content can help you. Many PHPer will encounter some problems and bottlenecks when they are advanced, and they have no sense of direction when writing too many business codes. I have sorted out some information, including but not limited to: Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, Laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell scripting, Docker, microservices, Nginx, etc.