laravel5異常及時通知

項目上線後,都會使用一些異常監控,當然很多時候監控是有限制的,比如要監控PHP異常,類似這種一般都在輸出人性化內容而不是直接輸出錯誤內容,很多時候需要捕捉這類異常來進行代碼調整。當然也可以定期去查看日誌。


laravel5支持自定義異常處理,給這種需求提供了方便,我們完全可以擴展異常處理,通過發送郵件或短信。

打開 app/Exceptions/Handler.php  文件,修改 render 函數代碼,增加發送郵件通知功能:

if (!$e instanceof \Symfony\Component\Debug\Exception\FlattenException) {
    $e = \Symfony\Component\Debug\Exception\FlattenException::create($e);
}
$exception = new \Symfony\Component\Debug\ExceptionHandler();
$content = $exception->getContent($e);
$css = $exception->getStylesheet($e);
\Mail::queue('errors.mail', [
    'url' => $request->fullUrl(),
    'request' => $request->all(),
    'method' => $request->getMethod(),
    'header' => $request->header(),
    'content' => $content,
    'css' => $css
        ], function ($message) {
    $message->to('[email protected]')
            ->cc('[email protected]')
            ->subject('程序異常');
});

原來的

return parent::render($request, $e);

是返回異常內容或403頁面的,如果想頁面返回更友好,可以去掉這個代碼改成其它內容返回,可直接使用如形式的代碼:

return response('服務器累了,稍後再試!');


郵件模板:

<HTML>
    <HEAD>
        <TITLE>系統異常,請及時維護!</TITLE>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <STYLE>
            html{color:#000;background:#FFF;}
            body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,textarea,p,blockquote,th,td{margin:0;padding:0;}
            table{border-collapse:collapse;border-spacing:0;}
            fieldset,img{border:0;}
            address,caption,cite,code,dfn,em,strong,th,var{font-style:normal;font-weight:normal;}
            li{list-style:none;}caption,th{text-align:left;}
            q:before,q:after{content:'';}
            abbr,acronym{border:0;font-variant:normal;}
            sup{vertical-align:text-top;}
            sub{vertical-align:text-bottom;}
            input,textarea,select{font-family:inherit;font-size:inherit;font-weight:inherit;}
            input,textarea,select{*font-size:100%;}
            legend{color:#000;}
            html { background: #eee; padding: 10px }
            img { border: 0; }
            #sf-resetcontent { width:970px; margin:0 auto; }
            {!!$css!!}
        </style>
    </HEAD>
    <BODY>
        <h2>請求地址:</h2>
        {{$url}} &nbsp;&nbsp;&nbsp;
        <h2>請求方式:</h2>
        {{$method}}
        <h2>請求參數:</h2>
        <pre>
            {{var_export($request, true)}}
        </pre>
        <h2>請求頭信息:</h2>
        <pre>
            {{var_export($header, true)}}
        </pre>
        <h2>異常內容:</h2>
        <pre>
            {!!$content!!}
        </pre>
    </BODY>
</HTML>


注意:郵件是通過隊列發送的,以減少頁面響應速度,實際使用時需要開啓隊列處理命令。


開啓隊列處理命令方法:

  1. 直接添加定時命令到 crontab

    crontab -e
    * * * * * php artisan queue:work 1>> /dev/null 2>&1  #啓動隊列監聽
  2. 寫到框架的 schedule 中,然後通過 schedule 模擬crontab定時處理內部所有命令

    打開 app/Console/Kernel.php 文件在 schedule 函數下添加代碼:

    //監聽隊列

    $schedule->command('queue:work',$parameters)
        ->everyMinute()
        ->withoutOverlapping();

    然後添加定時命令:

    crontab -e
    * * * * * php artisan schedule:run 1>> /dev/null 2>&1   #啓動schedule


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章