首页 > Web开发 > 详细

【PHP错误捕获分享】PHP捕获应用错误 简单记录并友好处理展示

时间:2015-10-01 12:54:48      阅读:195      评论:0      收藏:0      [点我收藏+]

为了某种目的而需要捕获系统错误,在此简单分享下某个项目中的捕获方法。

默认php错误如图

技术分享

index.php

/**
 * 捕获应用错误记录
 */
function ef_error_record($errno,$errstr,$errfile,$errline){
	$e = array("no"=>$errno,"str"=>$errstr,"file"=>$errfile,"line"=>$errline);
        ef_error_display("record",$e);
}
/**
 * 错误中断显示
 */
function ef_error_display($method=‘‘,$option=‘‘){
	static $e = array();
	if($method==‘record‘){
		$e[] =  $option;
		return true;
	}else if(!empty($e)){
		include (‘/common/template/error.php‘);
		exit();
	}
	return true;
}
        
        
        //禁止错误输出
        error_reporting(0);
        //设置自定义错误函数 捕获系统错误并记录
	set_error_handler("ef_error_record");
	//注册结束脚本函数 该函数会有意外或正常结束脚本触发(同析构函数)
	register_shutdown_function("ef_error_display");


error.php

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>EFrame 运行错误</title>
<style type="text/css">

body {
background-color:	#fff;
margin:				40px;
font-family:		Lucida Grande, Verdana, Sans-serif;
font-size:			12px;
color:				#000;
}

#content  {
border:				#999 1px solid;
background-color:	#fff;
padding:			20px 20px 12px 20px;
}

h1 {
font-weight:		normal;
font-size:			16px;
color:				#990000;
margin: 			0 0 4px 0;
}
span {
color:				#990000;
	
}
.msg p{
	color:#990000;
}
.file{
	text-indent:15px;;
}
</style>
</head>
<body>
	<div id="content">
		<h1>EFrame 运行错误</h1>
		<?php
			foreach($e as $key=>$val){
				echo "<div class=\"msg\"><p><span>[".($key+1)."]</span> {$val[‘str‘]} </p></div>",
				"<div class=\"file\"><p>".$val[‘file‘]." 行: <span>{$val[‘line‘]}</span></p></div>";
			}
			?>	</div>
</body>
</html>

最终错误错误页面(当然页面可以自己修改想要的样式):

技术分享

【PHP错误捕获分享】PHP捕获应用错误 简单记录并友好处理展示

原文:http://my.oschina.net/u/2366984/blog/513065

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!