分享一個php 的例外處理常式_PHP教程

來源:互聯網
上載者:User
直接上代碼

<?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');}?>

http://www.bkjia.com/PHPjc/825533.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/825533.htmlTechArticle直接上代碼 php//exceptionHandle.php xiecongwen 20140620//define('DEBUG',true);/*** Display all errors when APPLICATION_ENV is development.*/if (defined('DEBUG')) {error_report...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.