- <?php
- //禁止错误输出
- error_reporting(0);
- //设置错误处理器
- set_error_handler('errorHandler');
- register_shutdown_function('fatalErrorHandler');
- class Test{
- public function index(){
- //这里发生一个警告错误,出发errorHandler
- echo $undefinedVarible;
- }
- }
- function errorHandler($errno,$errstr,$errfile,$errline){
- $arr = array(
- '['.date('Y-m-d h-i-s').']',
- 'http://www.baidu.com',
- '|',
- $errstr,
- $errfile,
- 'line:'.$errline,
- );
- //写入错误日志
- //格式 : 时间 uri | 错误消息 文件位置 第几行
- error_log(implode(' ',$arr)."\\r\\n",3,'./test.txt','extra');
- echo implode(' ',$arr)."\\r\\n";
- }
- //捕获fatalError
- function fatalErrorHandler(){
- $e = error_get_last();
- switch($e['type']){
- case E_ERROR:
- case E_PARSE:
- case E_CORE_ERROR:
- case E_COMPILE_ERROR:
- case E_USER_ERROR:
- errorHandler($e['type'],$e['message'],$e['file'],$e['line']);
- break;
- }
- }
- $test = new Test();
- ////这里发生一个警告错误,被errorHandler 捕获
- $test->index();
- //发生致命错误,脚本停止运行触发 fatalErrorHandler
- $test = new Tesdt();
- $test->index();
- //该片段来自于http://www.codesnippet.cn/detail/180320149069.html
来源: http://www.codesnippet.cn/detail/180320149069.html