laravel5异常及时通知

项目上线后,都会使用一些异常监控,固然不少时候监控是有限制的,好比要监控PHP异常,相似这种通常都在输出人性化内容而不是直接输出错误内容,不少时候须要捕捉这类异常来进行代码调整。固然也能够按期去查看日志。php


laravel5支持自定义异常处理,给这种需求提供了方便,咱们彻底能够扩展异常处理,经过发送邮件或短信。css

打开 app/Exceptions/Handler.php  文件,修改 render 函数代码,增长发送邮件通知功能:
html

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('name@admin.com')
            ->cc('name1@admin.com')
            ->subject('程序异常');
});

原来的laravel

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

是返回异常内容或403页面的,若是想页面返回更友好,能够去掉这个代码改为其它内容返回,可直接使用如形式的代码:bash

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>


注意:邮件是经过队列发送的,以减小页面响应速度,实际使用时须要开启队列处理命令。app


开启队列处理命令方法:框架

  1. 直接添加定时命令到 crontabide

    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
相关文章
相关标签/搜索