亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

分享一個(gè)php 的異常處理程序

 更新時(shí)間:2014年06月22日 11:01:21   投稿:hebedich  
給大家分享一個(gè)php的異常處理程序,功能很簡(jiǎn)單當(dāng)發(fā)生重大錯(cuò)誤時(shí),寫日志并友好提示用戶,用處卻很大,對(duì)不對(duì)

直接上代碼

<?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')){
/**
* 當(dāng)發(fā)生重大錯(cuò)誤時(shí) 寫日志 并友好提示用戶
* (PS:只所以將代碼寫在這里,是因?yàn)樵谄渌胤阶?cè)時(shí),出現(xiàn)問題無法調(diào)用配置函數(shù).待完善...)
*/
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('對(duì)不起,我出錯(cuò)了!');
}
}
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)
{
//獲取最準(zhǔn)確的異常 
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');
}
?>

相關(guān)文章

最新評(píng)論