分享一个php 的异常处理程序
更新时间:2014年06月22日 11:01:21 投稿:hebedich
给大家分享一个php的异常处理程序,功能很简单当发生重大错误时,写日志并友好提示用户,用处却很大,对不对
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud)
(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun)
直接上代码
<?php //exceptionHandle.php xiecongwen 20140620 //define('DEBUG',true); /** * Display all errors when APPLICATION_ENV is development. */ if (defined('DEBUG')) { error_reporting(E_ALL); ini_set("display_errors", 1); } if(!defined('DEBUG')){ /** * 当发生重大错误时 写日志 并友好提示用户 * (PS:只所以将代码写在这里,是因为在其他地方注册时,出现问题无法调用配置函数.待完善...) */ function shutdownHandler() { /** * 写日志 此处直接写在根目录下shutdownlog.txt */ $lasterror = error_get_last(); if($lasterror){ $error = strval(date("Y-m-d h:i:s")).'=>'."[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); } function 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'); } ?>
相关文章
ThinkPHP框架整合微信支付之Native 扫码支付模式一图文详解
这篇文章主要介绍了ThinkPHP框架整合微信支付之Native 扫码支付模式一,结合图文形式详细分析了thinkPHP整合微信支付接口的扫码支付功能相关操作步骤、实现技巧与注意事项,需要的朋友可以参考下2019-04-04PHP SPL标准库之数据结构堆(SplHeap)简单使用实例
这篇文章主要介绍了PHP SPL标准库之数据结构堆(SplHeap)简单使用实例,本文还同时讲解了最大堆(SplMaxHeap)、最小堆(SplMinHeap)的相关知识,需要的朋友可以参考下2015-05-05Thinkphp+smarty+uploadify实现无刷新上传
这篇文章主要介绍了Thinkphp+smarty+uploadify实现无刷新上传的方法,实例分析了php模板与js上传插件结合实现无刷新上传的相关技巧,需要的朋友可以参考下2015-07-07
最新评论