PHP System exception handling program

Source: Internet
Author: User
Tags php debug
Previously we used a single exception handling function. below I found a very good exception handling system, which not only controls errors but also provides a good interface .? Set_exception _... previously we used a single exception handling function. below I found a very good exception handling system, which not only controls errors but also provides a good interface.

 '. $ Message .'
PHP:'. $ LogTrace; self: writeErrorLog ($ messageSave);} if ($ show) {self: showError ('system ',"
  • $ Message
  • ", $ ShowTrace, 0) ;}if ($ halt) {exit () ;}else {return $ message ;}} /*** code execution backtracking information ** @ static * @ access public */public static function debugBacktrace () {$ skipFunc [] = 'Error-> debugBacktrace '; $ show = $ log = ''; $ debugBacktrace = debug_backtrace (); ksort ($ debugBacktrace); foreach ($ debugBacktrace as $ k => $ error) {if (! Isset ($ error ['file']) {// use the Reflection API to obtain the method/function file and number of lines try {if (isset ($ error ['class']) {$ reflection = new ReflectionMethod ($ error ['class'], $ error ['function']);} else {$ reflection = new ReflectionFunction ($ error ['function']);} $ error ['file'] = $ reflection-> getFileName (); $ error ['line'] = $ reflection-> getStartLine ();} catch (Exception $ e) {continue ;}$ file = str_replace (SITE_PATH ,'', $ error ['File']); $ func = isset ($ error ['class'])? $ Error ['class']: ''; $ func. = isset ($ error ['type'])? $ Error ['type']: ''; $ func. = isset ($ error ['function'])? $ Error ['function']: ''; if (in_array ($ func, $ skipFunc) {break ;} $ error ['line'] = sprintf ('% 04d', $ error ['line']); $ show. ='
  • [Line: '. $ error ['line'].'] '. $ file.' ('. $ func .')
  • '; $ Log. =! Empty ($ log )? '->': ''; $ Log. = $ file. ':'. $ error ['line'];} return array ($ show, $ log );} /*** exception handling *** @ static * @ access public * @ param mixed $ exception */public static function exceptionError ($ exception) {if ($ exception instanceof DbException) {$ type = 'DB';} else {$ type = 'system';} if ($ type = 'DB') {$ errorMsg = '('. $ exception-> getCode (). ')'; $ errorMsg. = self: sqlClear ($ exception-> getMessage (), $ exception-> getDbConfig (); if ($ exception-> getSql () {$ errorMsg. ='

    '; $ ErrorMsg. = self: sqlClear ($ exception-> getSql (), $ exception-> getDbConfig (); $ errorMsg. ='

    ';}} Else {$ errorMsg = $ exception-> getMessage () ;}$ trace = $ exception-> getTrace (); krsort ($ trace ); $ trace [] = array ('file' => $ exception-> getFile (), 'line' => $ exception-> getLine (), 'function' => 'break'); $ phpMsg = array (); foreach ($ trace as $ error) {if (! Empty ($ error ['function']) {$ fun = ''; if (! Empty ($ error ['class']) {$ fun. = $ error ['class']. $ error ['type'];} $ fun. = $ error ['function']. '('; if (! Empty ($ error ['args ']) {$ mark = ''; foreach ($ error ['args'] as $ arg) {$ fun. = $ mark; if (is_array ($ arg) {$ fun. = 'array';} elseif (is_bool ($ arg) {$ fun. = $ arg? 'True': 'false';} elseif (is_int ($ arg) {$ fun. = (defined ('site _ debug') & SITE_DEBUG )? $ Arg: '% d';} elseif (is_float ($ arg) {$ fun. = (defined ('site _ debug') & SITE_DEBUG )? $ Arg: '% f';} else {$ fun. = (defined ('site _ debug') & SITE_DEBUG )? '''. Htmlspecialchars (substr (self: clear ($ arg), 0, 10). (strlen ($ arg)> 10? '... ':''). ''': '% S';} $ mark =', ';}}$ fun. = ')'; $ error ['function'] = $ fun;} if (! Isset ($ error ['line']) {continue;} $ phpMsg [] = array ('file' => str_replace (array (SITE_PATH ,''), array ('', '/'), $ error ['file']), 'line' => $ error ['line'], 'function' => $ error ['function']);} self: showError ($ type, $ errorMsg, $ phpMsg); exit ();} /*** record error log ** @ static * @ access public * @ param string $ message */public static function writeErrorLog ($ message) {return false; // do not write at the moment http://www.phprm.com $ Message = self: clear ($ message); $ time = time (); $ file = LOG_PATH. '/'. date ('Y. m. D '). '_ errorlog. php '; $ hash = md5 ($ message); $ userId = 0; $ ip = get_client_ip (); $ user ='User:UserId = '. intval ($ userId ). '; IP = '. $ ip. '; RIP :'. $ _ SERVER ['remote _ ADDR ']; $ uri = 'Request :'. htmlspecialchars (self: clear ($ _ SERVER ['request _ URI ']); $ message =" {$ Time} $ message $ hash $ user $ uri "; // determines whether the $ message has been recorded in the interval $ maxtime, no need to record if (is_file ($ file) {$ fp = @ fopen ($ file, 'RB'); $ lastlen = 50000; // read the last $ lastlen length byte content $ maxtime = 60*10; // time interval: 10 minutes $ offset = filesize ($ file)-$ lastlen; if ($ offset> 0) {fseek ($ fp, $ offset);} if ($ data = fread ($ fp, $ lastlen )) {$ array = explode ("", $ data); if (is_array ($ array) foreach ($ array as $ key => $ Val) {$ row = explode ("", $ val); if ($ row [0]! =' ') {Continue;} if ($ row [3] ==$ hash & ($ row [1]> $ time-$ maxtime) {return ;}}}} error_log ($ message, 3, $ file);}/*** clear some text characters ** @ param string $ message */public static function clear ($ message) {return str_replace (array ("", "", ""), "", $ message );} /*** clear SQL statement characters ** @ static * @ access public * @ param string $ message * @ param string $ dbConfig */public static function sqlClear ($ Message, $ dbConfig) {$ message = self: clear ($ message); if (! (Defined ('site _ debug') & SITE_DEBUG) {$ message = str_replace ($ dbConfig ['database'], '***', $ message ); // $ message = str_replace ($ dbConfig ['prefix'], '***', $ message); $ message = str_replace (C ('Db _ prefix '), '***', $ message);} $ message = htmlspecialchars ($ message); return $ message ;} /*** display error ** @ static * @ access public * @ param string $ type error type db, system * @ param string $ errorMsg * @ param st Ring $ phpMsg */public static function showError ($ type, $ errorMsg, $ phpMsg = '') {global $ _ G; $ errorMsg = str_replace (SITE_PATH ,'', $ errorMsg); ob_end_clean (); $ host = $ _ SERVER ['http _ host']; $ title = $ type = 'DB '? 'Database': 'system'; echo < $ Host-$ title Error

    $ Title Error

    $ ErrorMsg

    EOT; if (! Empty ($ phpMsg) {echo'

    '; Echo'

    PHP Debug

    '; Echo' '; If (is_array ($ phpMsg) {echo' '; Foreach ($ phpMsg as $ k => $ msg) {$ k ++; echo' '; Echo' '; Echo' '; Echo' '; Echo' '; Echo' ';}} Else {echo' ';} Echo'
    No. File Line Code
    '. $ K .''. $ Msg ['file'].''. $ Msg ['line'].''. $ Msg ['function'].'
      '. $ PhpMsg .'

    ';} Echo < EOT; exit () ;}}/*** DB Exception class ** @ author www.phprm.com */class DbException extends Exception {protected $ SQL; protected $ dbConfig; // Current database configuration information public function _ construct ($ message, $ code = 0, $ SQL = '', $ dbConfig = array ()) {$ this-> SQL = $ SQL; $ this-> dbConfig = $ dbConfig; parent ::__ construct ($ message, $ code);} public function getSql () {return $ this-> SQL;} public function getDbConfig () {return $ This-> dbConfig ;}}?>

    : PHP System exception handling program

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.