首頁 後端開發 php教程 拦截PHP各种错误和异常,发生致命异常时进行报警,万事防患于未然

拦截PHP各种错误和异常,发生致命异常时进行报警,万事防患于未然

Jun 13, 2016 pm 12:28 PM
error exception function gt warning

拦截PHP各种异常和错误,发生致命错误时进行报警,万事防患于未然

在日常开发中,大多数人的做法是在开发环境时开启调试模式,在产品环境关闭调试模式。在开发的时候可以查看各种错误、异常,但是在线上就把错误显示的关闭。

上面的情形看似很科学,有人解释为这样很安全,别人看不到错误,以免泄露重要信息...

但是你有没有遇到这种情况,线下好好的,一上线却运行不起来也找不到原因...

一个脚本,跑了好长一段时间,一直没有问题,有一天突然中断了,然后了也没有任何记录都不造啥原因...

线上一个付款,别人明明付了款,但是我们却没有记录到,自己亲自去实验,却是好的...

 

种种以上,都是因为大家关闭了错误信息,并且未将错误、异常记录到日志,导致那些随机发生的错误很难追踪。这样矛盾就来了,即不要显示错误,又要追踪错误,这如何实现了?

以上问题都可以通过PHP的错误、异常机制及其内建函数'set_exception_handler','set_error_handler','register_shutdown_function' 来实现

 

'set_exception_handler' 函数 用于拦截各种未捕获的异常,然后将这些交给用户自定义的方式进行处理

'set_error_handler' 函数可以拦截各种错误,然后交给用户自定义的方式进行处理

'register_shutdown_function' 函数是在PHP脚本结束时调用的函数,配合'error_get_last'可以获取最后的致命性错误

 

这个思路大体就是把错误、异常、致命性错误拦截下来,交给我们自定义的方法进行处理,我们辨别这些错误、异常是否致命,如果是则记录的数据库或者文件系统,然后使用脚本不停的扫描这些日志,发现严重错误立即发送邮件或发送短信进行报警

 

首先我们定义错误拦截类,该类用于将错误、异常拦截下来,用我们自己定义的处理方式进行处理,该类放在文件名为'errorHandler.class.php'中,代码如下

<span style="color: #008000;">/*</span><span style="color: #008000;">* * 文件名称:baseErrorHandler.class.php * 摘    要:错误拦截器父类 </span><span style="color: #008000;">*/</span><span style="color: #0000ff;">require</span> 'errorHandlerException.class.php';<span style="color: #008000;">//</span><span style="color: #008000;">异常类</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> errorHandler{    </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$argvs</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();    </span><span style="color: #0000ff;">public</span>     <span style="color: #800080;">$memoryReserveSize</span> = 262144;<span style="color: #008000;">//</span><span style="color: #008000;">备用内存大小</span>    <span style="color: #0000ff;">private</span> <span style="color: #800080;">$_memoryReserve</span>;<span style="color: #008000;">//</span><span style="color: #008000;">备用内存</span>    <span style="color: #008000;">/*</span><span style="color: #008000;">*     * 方      法:注册自定义错误、异常拦截器     * 参      数:void     * 返      回:void     </span><span style="color: #008000;">*/</span>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> register()    {        </span><span style="color: #008080;">ini_set</span>('display_errors', 0<span style="color: #000000;">);        </span><span style="color: #008080;">set_exception_handler</span>(<span style="color: #0000ff;">array</span>(<span style="color: #800080;">$this</span>, 'handleException'));<span style="color: #008000;">//</span><span style="color: #008000;">截获未捕获的异常</span>        <span style="color: #008080;">set_error_handler</span>(<span style="color: #0000ff;">array</span>(<span style="color: #800080;">$this</span>, 'handleError'));<span style="color: #008000;">//</span><span style="color: #008000;">截获各种错误 此处切不可掉换位置        //留下备用内存 供后面拦截致命错误使用</span>        <span style="color: #800080;">$this</span>->memoryReserveSize > 0 && <span style="color: #800080;">$this</span>->_memoryReserve = <span style="color: #008080;">str_repeat</span>('x', <span style="color: #800080;">$this</span>-><span style="color: #000000;">memoryReserveSize);        </span><span style="color: #008080;">register_shutdown_function</span>(<span style="color: #0000ff;">array</span>(<span style="color: #800080;">$this</span>, 'handleFatalError'));<span style="color: #008000;">//</span><span style="color: #008000;">截获致命性错误</span><span style="color: #000000;">    }    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*     * 方      法:取消自定义错误、异常拦截器     * 参      数:void     * 返      回:void     </span><span style="color: #008000;">*/</span>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> unregister()    {        </span><span style="color: #008080;">restore_error_handler</span><span style="color: #000000;">();        </span><span style="color: #008080;">restore_exception_handler</span><span style="color: #000000;">();    }    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*     * 方      法:处理截获的未捕获的异常     * 参      数:Exception $exception     * 返      回:void     </span><span style="color: #008000;">*/</span>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> handleException(<span style="color: #800080;">$exception</span><span style="color: #000000;">)    {        </span><span style="color: #800080;">$this</span>-><span style="color: #000000;">unregister();        </span><span style="color: #0000ff;">try</span><span style="color: #000000;">        {            </span><span style="color: #800080;">$this</span>->logException(<span style="color: #800080;">$exception</span><span style="color: #000000;">);            </span><span style="color: #0000ff;">exit</span>(1<span style="color: #000000;">);        }        </span><span style="color: #0000ff;">catch</span>(<span style="color: #0000ff;">Exception</span> <span style="color: #800080;">$e</span><span style="color: #000000;">)        {            </span><span style="color: #0000ff;">exit</span>(1<span style="color: #000000;">);        }    }    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*     * 方      法:处理截获的错误     * 参      数:int     $code 错误代码     * 参      数:string $message 错误信息     * 参      数:string $file 错误文件     * 参      数:int     $line 错误的行数     * 返      回:boolean     </span><span style="color: #008000;">*/</span>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> handleError(<span style="color: #800080;">$code</span>, <span style="color: #800080;">$message</span>, <span style="color: #800080;">$file</span>, <span style="color: #800080;">$line</span><span style="color: #000000;">)    {        </span><span style="color: #008000;">//</span><span style="color: #008000;">该处思想是将错误变成异常抛出 统一交给异常处理函数进行处理</span>        <span style="color: #0000ff;">if</span>((<span style="color: #008080;">error_reporting</span>() & <span style="color: #800080;">$code</span>) && !<span style="color: #008080;">in_array</span>(<span style="color: #800080;">$code</span>, <span style="color: #0000ff;">array</span>(<span style="color: #ff00ff;">E_NOTICE</span>, <span style="color: #ff00ff;">E_WARNING</span>, <span style="color: #ff00ff;">E_USER_NOTICE</span>, <span style="color: #ff00ff;">E_USER_WARNING</span>,<span style="color: #000000;"> E_DEPRECATED)))        {</span><span style="color: #008000;">//</span><span style="color: #008000;">此处只记录严重的错误 对于各种WARNING NOTICE不作处理</span>            <span style="color: #800080;">$exception</span> = <span style="color: #0000ff;">new</span> errorHandlerException(<span style="color: #800080;">$message</span>, <span style="color: #800080;">$code</span>, <span style="color: #800080;">$code</span>, <span style="color: #800080;">$file</span>, <span style="color: #800080;">$line</span><span style="color: #000000;">);            </span><span style="color: #800080;">$trace</span> = <span style="color: #008080;">debug_backtrace</span><span style="color: #000000;">(DEBUG_BACKTRACE_IGNORE_ARGS);            </span><span style="color: #008080;">array_shift</span>(<span style="color: #800080;">$trace</span>);<span style="color: #008000;">//</span><span style="color: #008000;">trace的第一个元素为当前对象 移除</span>            <span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$trace</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$frame</span><span style="color: #000000;">)             {                </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$frame</span>['function'] == '__toString'<span style="color: #000000;">)                 {</span><span style="color: #008000;">//</span><span style="color: #008000;">如果错误出现在 __toString 方法中 不抛出任何异常</span>                    <span style="color: #800080;">$this</span>->handleException(<span style="color: #800080;">$exception</span><span style="color: #000000;">);                    </span><span style="color: #0000ff;">exit</span>(1<span style="color: #000000;">);                }            }            </span><span style="color: #0000ff;">throw</span> <span style="color: #800080;">$exception</span><span style="color: #000000;">;        }        </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;    }    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*     * 方      法:截获致命性错误     * 参      数:void     * 返      回:void     </span><span style="color: #008000;">*/</span>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> handleFatalError()    {        </span><span style="color: #0000ff;">unset</span>(<span style="color: #800080;">$this</span>->_memoryReserve);<span style="color: #008000;">//</span><span style="color: #008000;">释放内存供下面处理程序使用</span>        <span style="color: #800080;">$error</span> = error_get_last();<span style="color: #008000;">//</span><span style="color: #008000;">最后一条错误信息</span>        <span style="color: #0000ff;">if</span>(errorHandlerException::isFatalError(<span style="color: #800080;">$error</span><span style="color: #000000;">))        {</span><span style="color: #008000;">//</span><span style="color: #008000;">如果是致命错误进行处理</span>            <span style="color: #800080;">$exception</span> = <span style="color: #0000ff;">new</span> errorHandlerException(<span style="color: #800080;">$error</span>['message'], <span style="color: #800080;">$error</span>['type'], <span style="color: #800080;">$error</span>['type'], <span style="color: #800080;">$error</span>['file'], <span style="color: #800080;">$error</span>['line'<span style="color: #000000;">]);            </span><span style="color: #800080;">$this</span>->logException(<span style="color: #800080;">$exception</span><span style="color: #000000;">);            </span><span style="color: #0000ff;">exit</span>(1<span style="color: #000000;">);        }    }    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*     * 方      法:获取服务器IP     * 参      数:void     * 返      回:string     </span><span style="color: #008000;">*/</span>    <span style="color: #0000ff;">final</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getServerIp()    {        </span><span style="color: #800080;">$serverIp</span> = ''<span style="color: #000000;">;        </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$_SERVER</span>['SERVER_ADDR'<span style="color: #000000;">]))        {            </span><span style="color: #800080;">$serverIp</span> = <span style="color: #800080;">$_SERVER</span>['SERVER_ADDR'<span style="color: #000000;">];        }        </span><span style="color: #0000ff;">elseif</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$_SERVER</span>['LOCAL_ADDR'<span style="color: #000000;">]))        {            </span><span style="color: #800080;">$serverIp</span> = <span style="color: #800080;">$_SERVER</span>['LOCAL_ADDR'<span style="color: #000000;">];        }        </span><span style="color: #0000ff;">elseif</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$_SERVER</span>['HOSTNAME'<span style="color: #000000;">]))        {            </span><span style="color: #800080;">$serverIp</span> = <span style="color: #008080;">gethostbyname</span>(<span style="color: #800080;">$_SERVER</span>['HOSTNAME'<span style="color: #000000;">]);        }        </span><span style="color: #0000ff;">else</span><span style="color: #000000;">        {            </span><span style="color: #800080;">$serverIp</span> = <span style="color: #008080;">getenv</span>('SERVER_ADDR'<span style="color: #000000;">);        }                        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$serverIp</span><span style="color: #000000;">;     }    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*     * 方      法:获取当前URI信息     * 参      数:void     * 返      回:string $url     </span><span style="color: #008000;">*/</span>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getCurrentUri()    {        </span><span style="color: #800080;">$uri</span> = ''<span style="color: #000000;">;        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$_SERVER</span> ["REMOTE_ADDR"<span style="color: #000000;">])        {</span><span style="color: #008000;">//</span><span style="color: #008000;">浏览器浏览模式</span>            <span style="color: #800080;">$uri</span> = 'http://' . <span style="color: #800080;">$_SERVER</span>['SERVER_NAME'] . <span style="color: #800080;">$_SERVER</span>['REQUEST_URI'<span style="color: #000000;">];        }        </span><span style="color: #0000ff;">else</span><span style="color: #000000;">        {</span><span style="color: #008000;">//</span><span style="color: #008000;">命令行模式</span>            <span style="color: #800080;">$params</span> = <span style="color: #800080;">$this</span>-><span style="color: #000000;">argvs;            </span><span style="color: #800080;">$uri</span> = <span style="color: #800080;">$params</span>[0<span style="color: #000000;">];            </span><span style="color: #008080;">array_shift</span>(<span style="color: #800080;">$params</span><span style="color: #000000;">);            </span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span> = 0, <span style="color: #800080;">$len</span> = <span style="color: #008080;">count</span>(<span style="color: #800080;">$params</span>); <span style="color: #800080;">$i</span> $len; <span style="color: #800080;">$i</span>++<span style="color: #000000;">)            {                </span><span style="color: #800080;">$uri</span> .= ' ' . <span style="color: #800080;">$params</span>[<span style="color: #800080;">$i</span><span style="color: #000000;">];            }        }        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$uri</span><span style="color: #000000;">;    }    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*     * 方      法:记录异常信息     * 参      数:errorHandlerException $e 错误异常     * 返      回:boolean 是否保存成功     </span><span style="color: #008000;">*/</span>    <span style="color: #0000ff;">final</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> logException(<span style="color: #800080;">$e</span><span style="color: #000000;">)    {        </span><span style="color: #800080;">$error</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(                        </span>'add_time'     =>     <span style="color: #008080;">time</span>(),                        'title'     =>     errorHandlerException::getName(<span style="color: #800080;">$e</span>->getCode()),<span style="color: #008000;">//</span><span style="color: #008000;">这里获取用户友好型名称</span>                        'message'     =>     <span style="color: #0000ff;">array</span>(),                        'server_ip' =>     <span style="color: #800080;">$this</span>->getServerIp(),                        'code'         =>     errorHandlerException::getLocalCode(<span style="color: #800080;">$e</span>->getCode()),<span style="color: #008000;">//</span><span style="color: #008000;">这里为各种错误定义一个编号以便查找</span>                        'file'         =>  <span style="color: #800080;">$e</span>->getFile(),                        'line'         =>     <span style="color: #800080;">$e</span>->getLine(),                        'url'        =>  <span style="color: #800080;">$this</span>->getCurrentUri(),<span style="color: #000000;">                    );        </span><span style="color: #0000ff;">do</span><span style="color: #000000;">        {            </span><span style="color: #008000;">//</span><span style="color: #008000;">$e->getFile() . ':' . $e->getLine() . ' ' . $e->getMessage() . '(' . $e->getCode() . ')'</span>            <span style="color: #800080;">$message</span> = (<span style="color: #0000ff;">string</span>)<span style="color: #800080;">$e</span><span style="color: #000000;">;            </span><span style="color: #800080;">$error</span>['message'][] = <span style="color: #800080;">$message</span><span style="color: #000000;">;        } </span><span style="color: #0000ff;">while</span>(<span style="color: #800080;">$e</span> = <span style="color: #800080;">$e</span>-><span style="color: #000000;">getPrevious());        </span><span style="color: #800080;">$error</span>['message'] = <span style="color: #008080;">implode</span>("\r\n", <span style="color: #800080;">$error</span>['message'<span style="color: #000000;">]);        </span><span style="color: #800080;">$this</span>->logError(<span style="color: #800080;">$error</span><span style="color: #000000;">);    }    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*     * 方      法:记录异常信息     * 参      数:array $error = array(     *                                    'time' => int,      *                                    'title' => 'string',      *                                    'message' => 'string',      *                                    'code' => int,     *                                    'server_ip' => 'string'     *                                     'file'     =>  'string',     *                                    'line' => int,     *                                    'url' => 'string',     *                                );     * 返      回:boolean 是否保存成功     </span><span style="color: #008000;">*/</span>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> logError(<span style="color: #800080;">$error</span><span style="color: #000000;">)    {        </span><span style="color: #008000;">/*</span><span style="color: #008000;">这里去实现如何将错误信息记录到日志</span><span style="color: #008000;">*/</span><span style="color: #000000;">    }}</span>
登入後複製

上述代码中,有个'errorHandlerException'类,该类放在文件'errorHandlerException.class.php'中,该类用于将错误转换为异常,以便记录错误发生的文件、行号、错误代码、错误信息等信息,同时其方法'isFatalError'用于辨别该错误是否是致命性错误。这里我们为了方便管理,将错误进行编号并命名。该类的代码如下

<span style="color: #008000;">/*</span><span style="color: #008000;">* * 文件名称:errorHandlerException.class.php * 摘    要:自定义错误异常类 该类继承至PHP内置的错误异常类 </span><span style="color: #008000;">*/</span><span style="color: #0000ff;">class</span> errorHandlerException <span style="color: #0000ff;">extends</span><span style="color: #000000;"> ErrorException{    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #800080;">$localCode</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(                                        </span><span style="color: #ff00ff;">E_COMPILE_ERROR</span> => 4001,                                        <span style="color: #ff00ff;">E_COMPILE_WARNING</span> => 4002,                                        <span style="color: #ff00ff;">E_CORE_ERROR</span> => 4003,                                        <span style="color: #ff00ff;">E_CORE_WARNING</span> => 4004,<span style="color: #000000;">                                        E_DEPRECATED </span>=> 4005,                                        <span style="color: #ff00ff;">E_ERROR</span> => 4006,                                        <span style="color: #ff00ff;">E_NOTICE</span> => 4007,                                        <span style="color: #ff00ff;">E_PARSE</span> => 4008,<span style="color: #000000;">                                        E_RECOVERABLE_ERROR </span>=> 4009,                                        <span style="color: #ff00ff;">E_STRICT</span> => 4010,<span style="color: #000000;">                                        E_USER_DEPRECATED </span>=> 4011,                                        <span style="color: #ff00ff;">E_USER_ERROR</span> => 4012,                                        <span style="color: #ff00ff;">E_USER_NOTICE</span> => 4013,                                        <span style="color: #ff00ff;">E_USER_WARNING</span> => 4014,                                        <span style="color: #ff00ff;">E_WARNING</span> => 4015,                                        4016 => 4016,<span style="color: #000000;">                                    );    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #800080;">$localName</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(                                        </span><span style="color: #ff00ff;">E_COMPILE_ERROR</span> => 'PHP Compile Error',                                        <span style="color: #ff00ff;">E_COMPILE_WARNING</span> => 'PHP Compile Warning',                                        <span style="color: #ff00ff;">E_CORE_ERROR</span> => 'PHP Core Error',                                        <span style="color: #ff00ff;">E_CORE_WARNING</span> => 'PHP Core Warning',<span style="color: #000000;">                                        E_DEPRECATED </span>=> 'PHP Deprecated Warning',                                        <span style="color: #ff00ff;">E_ERROR</span> => 'PHP Fatal Error',                                        <span style="color: #ff00ff;">E_NOTICE</span> => 'PHP Notice',                                        <span style="color: #ff00ff;">E_PARSE</span> => 'PHP Parse Error',<span style="color: #000000;">                                        E_RECOVERABLE_ERROR </span>=> 'PHP Recoverable Error',                                        <span style="color: #ff00ff;">E_STRICT</span> => 'PHP Strict Warning',<span style="color: #000000;">                                        E_USER_DEPRECATED </span>=> 'PHP User Deprecated Warning',                                        <span style="color: #ff00ff;">E_USER_ERROR</span> => 'PHP User Error',                                        <span style="color: #ff00ff;">E_USER_NOTICE</span> => 'PHP User Notice',                                        <span style="color: #ff00ff;">E_USER_WARNING</span> => 'PHP User Warning',                                        <span style="color: #ff00ff;">E_WARNING</span> => 'PHP Warning',                                        4016 => 'Customer`s Error',<span style="color: #000000;">                                    );    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*     * 方      法:构造函数     * 摘      要:相关知识请查看 http://php.net/manual/en/errorexception.construct.php     *        * 参      数:string        $message     异常信息(可选)     *              int         $code         异常代码(可选)     *              int         $severity     *              string     $filename     异常文件(可选)     *              int         $line         异常的行数(可选)     *           Exception  $previous   上一个异常(可选)     *     * 返      回:void     </span><span style="color: #008000;">*/</span>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$message</span> = '', <span style="color: #800080;">$code</span> = 0, <span style="color: #800080;">$severity</span> = 1, <span style="color: #800080;">$filename</span> = <span style="color: #ff00ff;">__FILE__</span>, <span style="color: #800080;">$line</span> = <span style="color: #ff00ff;">__LINE__</span>, <span style="color: #0000ff;">Exception</span> <span style="color: #800080;">$previous</span> = <span style="color: #0000ff;">null</span><span style="color: #000000;">)    {        parent</span>::__construct(<span style="color: #800080;">$message</span>, <span style="color: #800080;">$code</span>, <span style="color: #800080;">$severity</span>, <span style="color: #800080;">$filename</span>, <span style="color: #800080;">$line</span>, <span style="color: #800080;">$previous</span><span style="color: #000000;">);    }    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*     * 方      法:是否是致命性错误     * 参      数:array $error     * 返      回:boolean     </span><span style="color: #008000;">*/</span>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> isFatalError(<span style="color: #800080;">$error</span><span style="color: #000000;">)    {        </span><span style="color: #800080;">$fatalErrors</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(                                </span><span style="color: #ff00ff;">E_ERROR</span>,                                 <span style="color: #ff00ff;">E_PARSE</span>,                                 <span style="color: #ff00ff;">E_CORE_ERROR</span>,                                <span style="color: #ff00ff;">E_CORE_WARNING</span>,                                 <span style="color: #ff00ff;">E_COMPILE_ERROR</span>,                                 <span style="color: #ff00ff;">E_COMPILE_WARNING</span><span style="color: #000000;">                            );        </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$error</span>['type']) && <span style="color: #008080;">in_array</span>(<span style="color: #800080;">$error</span>['type'], <span style="color: #800080;">$fatalErrors</span><span style="color: #000000;">);    }    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*     * 方      法:根据原始的错误代码得到本地的错误代码     * 参      数:int $code     * 返      回:int $localCode     </span><span style="color: #008000;">*/</span>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> getLocalCode(<span style="color: #800080;">$code</span><span style="color: #000000;">)    {        </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">isset</span>(self::<span style="color: #800080;">$localCode</span>[<span style="color: #800080;">$code</span>]) ? self::<span style="color: #800080;">$localCode</span>[<span style="color: #800080;">$code</span>] : self::<span style="color: #800080;">$localCode</span>[4016<span style="color: #000000;">];    }    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*     * 方      法:根据原始的错误代码获取用户友好型名称     * 参      数:int      * 返      回:string $name     </span><span style="color: #008000;">*/</span>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> getName(<span style="color: #800080;">$code</span><span style="color: #000000;">)    {        </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">isset</span>(self::<span style="color: #800080;">$localName</span>[<span style="color: #800080;">$code</span>]) ? self::<span style="color: #800080;">$localName</span>[<span style="color: #800080;">$code</span>] : self::<span style="color: #800080;">$localName</span>[4016<span style="color: #000000;">];    }</span>
登入後複製

在错误拦截类中,需要用户自己定义实现错误记录的方法('logException'),这个地方需要注意,有些错误可能在一段时间内不断发生,因此只需记录一次即可,你可以使用错误代码、文件、行号、错误详情 生成一个MD5值用于记录该错误是否已经被记录,如果在规定时间内(一个小时)已经被记录过则不需要再进行记录

 

然后我们定义一个文件,用于实例化以上类,捕获各种错误、异常,该文件命名为'registerErrorHandler.php', 内如如下

<span style="color: #008000;">/*</span><span style="color: #008000;">* 使用方法介绍:* 在入口处引入该文件即可,然后可以在该文件中定义调试模式常量'DEBUG_ERROR'** <?php *   *    require 'registerErrorHandler.php';*   * ?></span><span style="color: #008000;">*/</span><span style="color: #008000;">/*</span><span style="color: #008000;">** 调试错误模式:* 0                =>            非调试模式,不显示异常、错误信息但记录异常、错误信息* 1                =>            调试模式,显示异常、错误信息但不记录异常、错误信息</span><span style="color: #008000;">*/</span><span style="color: #008080;">define</span>('DEBUG_ERROR', 0<span style="color: #000000;">);</span><span style="color: #0000ff;">require</span> 'errorHandler.class.php'<span style="color: #000000;">;</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> registerErrorHandler{    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*     * 方      法:注册异常、错误拦截     * 参      数:void     * 返      回:void     </span><span style="color: #008000;">*/</span>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> register()    {        </span><span style="color: #0000ff;">global</span> <span style="color: #800080;">$argv</span><span style="color: #000000;">;        </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(DEBUG_ERROR)        {</span><span style="color: #008000;">//</span><span style="color: #008000;">如果开启调试模式</span>            <span style="color: #008080;">ini_set</span>('display_errors', 1<span style="color: #000000;">);            </span><span style="color: #0000ff;">return</span><span style="color: #000000;">;        }        </span><span style="color: #008000;">//</span><span style="color: #008000;">如果不开启调试模式</span>        <span style="color: #008080;">ini_set</span>('error_reporting', -1<span style="color: #000000;">);        </span><span style="color: #008080;">ini_set</span>('display_errors', 0<span style="color: #000000;">);        </span><span style="color: #800080;">$handler</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> errorHandler();        </span><span style="color: #800080;">$handler</span>->argvs = <span style="color: #800080;">$argv</span>;<span style="color: #008000;">//</span><span style="color: #008000;">此处主要兼容命令行模式下获取参数</span>        <span style="color: #800080;">$handler</span>-><span style="color: #000000;">register();    }    }registerErrorHandler</span>::register();
登入後複製

剩下的就是需要你在你的入口文件引入该文件,定义调试模式,然后实现你自己记录错误的方法即可

需要注意的是,有些错误在你进行注册之前已经发生并且导致脚本中断是无法记录下来的,因为此时'registerErrorHandler::register()' 尚未执行已经中断了

还有就是'set_error_handler'这个函数不能捕获下面类型的错误 E_ERROR、 E_PARSE、 E_CORE_ERROR、 E_CORE_WARNINGE_COMPILE_ERROR、 E_COMPILE_WARNING, 这个可以在官方文档中看到,但是本处无妨,因为以上错误是解析、编译错误,这些都没有通过,你是不可能发布上线的

 

以上代码经过严格测试,并且已经应用在线上环境,大家可以根据自己需要进行更改使用

 

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1666
14
CakePHP 教程
1426
52
Laravel 教程
1328
25
PHP教程
1273
29
C# 教程
1253
24
華為GT3 Pro和GT4的差異是什麼? 華為GT3 Pro和GT4的差異是什麼? Dec 29, 2023 pm 02:27 PM

許多用戶在選擇智慧型手錶的時候都會選擇的華為的品牌,其中華為GT3pro和GT4都是非常熱門的選擇,不少用戶都很好奇華為GT3pro和GT4有什麼區別,下面就給大家介紹一下二者。華為GT3pro和GT4有什麼差別一、外觀GT4:46mm和41mm,材質是玻璃鏡板+不鏽鋼機身+高分纖維後殼。 GT3pro:46.6mm和42.9mm,材質是藍寶石玻璃鏡+鈦金屬機身/陶瓷機身+陶瓷後殼二、健康GT4:採用最新的華為Truseen5.5+演算法,結果會更加的精準。 GT3pro:多了ECG心電圖和血管及安

PHP Fatal error: Call to undefined method PDO::prepare() in的解決方法 PHP Fatal error: Call to undefined method PDO::prepare() in的解決方法 Jun 22, 2023 pm 06:40 PM

PHP作為一種流行的Web開發語言,已經被使用很久了。 PHP中整合的PDO(PHP資料物件)類別是我們在開發Web應用程式過程中與資料庫互動的常用方法。但是,一些PHP開發者經常遇到的問題是,當使用PDO類別與資料庫互動時,他們會收到這樣的錯誤:PHPFatalerror:CalltoundefinedmethodPDO::prep

在Vue應用程式中使用axios時出現「Uncaught (in promise) Error: Request failed with status code 500」怎麼辦? 在Vue應用程式中使用axios時出現「Uncaught (in promise) Error: Request failed with status code 500」怎麼辦? Jun 24, 2023 pm 05:33 PM

在Vue應用程式中使用axios是十分常見的,axios是一種基於Promise的HTTP客戶端,可以用於瀏覽器和Node.js。在開發過程中,有時會出現「Uncaught(inpromise)Error:Requestfailedwithstatuscode500」的錯誤提示,對於開發者來說,這個錯誤提示可能有些難以理解和解決。本文將會探討這

解決C++程式碼中出現的「error: expected initializer before 'datatype'」問題 解決C++程式碼中出現的「error: expected initializer before 'datatype'」問題 Aug 25, 2023 pm 01:24 PM

解決C++程式碼中出現的「error:expectedinitializerbefore'datatype'」問題在C++程式設計中,有時我們在寫程式碼時會遇到一些編譯錯誤,其中一個常見的錯誤是「error:expectedinitializerbefore'datatype'」。這個錯誤通常在變數宣告或函數定義中發生,可能導致程式無法正確編譯或

解決C++程式碼中出現的「error: incomplete type is not allowed」問題 解決C++程式碼中出現的「error: incomplete type is not allowed」問題 Aug 26, 2023 pm 08:54 PM

解決C++程式碼中出現的「error:incompletetypeisnotallowed」問題在C++的程式設計過程中,有時會遇到一些編譯錯誤,其中一個常見的錯誤是「error:incompletetypeisnotallowed」。這個錯誤通常是由於在使用不完整的類型進行操作時引起的。本文將介紹這個錯誤的原因,並提供幾種解決方法。首先,我

如何解決PHP Warning: fopen(): SSL operation failed in file.php on line X 如何解決PHP Warning: fopen(): SSL operation failed in file.php on line X Aug 25, 2023 am 09:22 AM

如何解決PHPWarning:fopen():SSLoperationfailedinfile.phponlineX在PHP程式設計中,我們經常使用fopen函數來開啟檔案或URL,並進行相關操作。然而,在使用fopen函數時,有時候會遇到類似Warning:fopen():SSLoperationfailedinfile.p

PHP Warning: Invalid argument supplied for foreach() - 解決方案 PHP Warning: Invalid argument supplied for foreach() - 解決方案 Aug 26, 2023 pm 09:42 PM

PHPWarning:Invalidargumentsuppliedforforeach()-解決方案在使用PHP開發網頁或應用程式時,經常會遇到各種錯誤和警告。其中一個常見的警告是“Invalidargumentsuppliedforforeach()”,它通常在使用foreach循環遍歷數組時產生。這個問題看起來很簡單,但如果不

function是什麼意思 function是什麼意思 Aug 04, 2023 am 10:33 AM

function是函數的意思,是一段具有特定功能的可重複使用的程式碼區塊,是程式的基本組成單元之一,可以接受輸入參數,執行特定的操作,並傳回結果,其目的是封裝一段可重複使用的程式碼,提高程式碼的可重複使用性和可維護性。

See all articles