Laravel has a very good exception handling mechanism, all Exceptions are handled by the report method of App\Exceptions\Handler (see details).

However, most companies and organizations I’ve seen have poor log viewing habits, and it’s possible to have a bunch of error logs or things that affect the user experience that go undetected.

You can choose to notify your website of an anomaly by email

Public function report($Exception, $Exception) {Sms::send($phone, [$Exception]); Mail::to($request->user())->send(new ExceptionNotify($exception)); return parent::report($exception); }Copy the code

Here about through wechat public notice, if you have a verified wechat service number, it is much more convenient, if not, you can zero cost to apply for a wechat test number. Click on me to apply directly

After entering, click add test template, title optional, template content can be copied below

{{first.DATA}} Exception type: {{keyword2.DATA}} Exception link: {{keyword2.DATA}} Access IP: {{keyword3.DATA}} {{remark.data}}Copy the code

Let’s start with an image (the one on the left can be defined arbitrarily, but here it is)

The following wechat package is super brother EasyWechat, you can understand.

If your application already relies on EasyWechat, it’s much easier

public function report(Exception $exception)
{
    app('wechat')->notice->to($userOpenId)->uses($templateId)->andUrl($url)->data($data)->send();
    return parent::report($exception);
}Copy the code

If you do not already rely on EasyWechat, you can choose to introduce wechat-notice

compooser require hanson/wechat-notice

Add Hanson \ WechatNotice \ NoticeServiceProvider: : class to app. PHP will

There are three ways to use it here

Pose a#

Define an array wechat in your config/services.php

return [
    // ...
    'wechat' => [
        'app_id' => 'your app id',
        'secret' => 'your secret'
    ]
]Copy the code
Public function report(Exception $Exception) {\WechatNotice::send($openId, $templateId, ['first' => 'system Exception ', 'keyword1' => get_class($this), 'keyword2' => url()->current(), 'keyword2' => $_SERVER['REMOTE_ADDR'], 'remark' => 'please deal with it promptly']); }Copy the code

Position 2#

Upload an existing wechat instance

public function report(Exception $exception) { $wechat = new Application(['app_id' => '', 'secret' => '']); \WechatNotice::setWechat($wechat)->send($openId, $templateId, 'keyword1' => get_class($this), 'keyword2' = > url () - > the current (), 'keyword2' = > $_SERVER [' REMOTE_ADDR], 'remark' = > 'please timely processing']); }Copy the code

Pose three#

Upload wechat parameters

public function report(Exception $exception) { \WechatNotice::setConfig(['app_id' => '', 'secret' = > ' ']) - > send ($openId, $templateId, [' first '= >' system exception ', 'keyword1' = > get_class ($this), 'keyword2' = > url () - > the current (), 'keyword2' = > $_SERVER [' REMOTE_ADDR], 'remark' = > 'please timely processing']); }Copy the code

Of course, you can also pass the OpenIDS array to notify multiple people

\WechatNotice::send([$openid1, $openid2], $templateId, $data, $url);Copy the code

If you’re a bit of a hooker, you can bind your account to authorization, click on the message template and display an error message for the current error, which is better than logging into the server and looking for a bunch of error logs

Fighting to advance PHP and Laravel, you can laugh, but I’m serious