分享一个php 的异常处理程序
直接上代码
<?php
//exceptionHandle.phpxiecongwen20140620
//define('DEBUG',true);
/**
*DisplayallerrorswhenAPPLICATION_ENVisdevelopment.
*/
if(defined('DEBUG')){
error_reporting(E_ALL);
ini_set("display_errors",1);
}
if(!defined('DEBUG')){
/**
*当发生重大错误时写日志并友好提示用户
*(PS:只所以将代码写在这里,是因为在其他地方注册时,出现问题无法调用配置函数.待完善...)
*/
functionshutdownHandler()
{
/**
*写日志此处直接写在根目录下shutdownlog.txt
*/
$lasterror=error_get_last();
if($lasterror){
$error=strval(date("Y-m-dh: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')){
functionerrorHandler($errno,$errstr='',$errfile='',$errline=0)
{
//写日志
$exception=new\ErrorException($errstr,0,$errno,$errfile,$errline);
$msg=strval(date("Y-m-dh:i:s")).'=>'.'Type:'.getErrTypeName($errno).''.getMsg($exception);
file_put_contents('./log/'.date("Ymd").'error.txt',$msg,FILE_APPEND);
switch($errno)
{
caseE_NOTICE:return;
caseE_DEPRECATED:return;
}
throw$exception;
}
functiongetErrTypeName($errno)
{
switch($errno)
{
caseE_NOTICE:return'E_NOTICE';
caseE_DEPRECATED:return'E_DEPRECATED';
default:return$errno;
}
}
functionexceptionHandler($ex)
{
$msg=strval(date("Y-m-dh:i:s")).'=>'.getMsg($ex);
file_put_contents('./log/'.date("Ymd").'exception.txt',$msg,FILE_APPEND);
}
functiongetMsg($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');
}
?>