一个php 错误处理程序

原创
2016-06-13 12:00:46 641浏览

一个php 异常处理程序

一个php的异常处理程序,功能很简单当发生重大错误时,写日志并友好提示用户.
代码:

'."[SHUTDOWN] lvl:" . $lasterror['type'] . " | msg:" . $lasterror['message'] . " | file:" . $lasterror['file'] . " | ln:" . $lasterror['line']."\n";file_put_contents('./log/'.date("Ymd").'shutdownlog.txt',$error,FILE_APPEND);//友好提示用户ob_end_clean();die('对不起,我出错了!');}}register_shutdown_function('shutdownHandler');}if(!defined('DEBUG')){ function errorHandler($errno, $errstr = '', $errfile = '', $errline = 0){//写日志$exception = new \ErrorException($errstr, 0, $errno, $errfile, $errline);$msg = strval(date("Y-m-d h:i:s")).'=>'.'Type:'.getErrTypeName($errno).' '.getMsg($exception);file_put_contents('./log/'.date("Ymd").'error.txt',$msg,FILE_APPEND);switch ($errno){case E_NOTICE:return ;case E_DEPRECATED:return;}throw $exception;}function getErrTypeName($errno){switch ($errno){case E_NOTICE:return 'E_NOTICE' ;case E_DEPRECATED:return 'E_DEPRECATED';default:return $errno;}}function exceptionHandler($ex){$msg = strval(date("Y-m-d h:i:s")).'=>'.getMsg($ex);file_put_contents('./log/'.date("Ymd").'exception.txt',$msg,FILE_APPEND);} www.jbxue.comfunction getMsg($exception){//获取最准确的异常 while($exception->getPrevious())$exception = $exception->getPrevious();$msg = ' Message: '.$exception->getMessage();$msg .= ' File: '.$exception->getFile().':'.$exception->getLine()."\n";return $msg;}set_error_handler('errorHandler',E_ALL);set_exception_handler('exceptionHandler');}?>

?

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。