首先,来看下YII框架里的handleError和handleException两个函数:
public function handleError($code,$message,$file,$line) { if($code & error_reporting()) { // disable error capturing to avoid recursive errors restore_error_handler(); restore_exception_handler(); $log="$message ($file:$line)\nStack trace:\n"; $trace=debug_backtrace(); // skip the first 3 stacks as they do not tell the error position if(count($trace)>3) $trace=array_slice($trace,3); //There is code to log trace massage try { Yii::import(‘CErrorEvent‘,true); $event=new CErrorEvent($this,$code,$message,$file,$line); $this->onError($event); if(!$event->handled) { // try an error handler if(($handler=$this->getErrorHandler())!==null) $handler->handle($event); else $this->displayError($code,$message,$file,$line); } } catch(Exception $e) { $this->displayException($e); } try { $this->end(1); } catch(Exception $e) { // use the most primitive way to log error $msg = get_class($e).‘: ‘.$e->getMessage().‘ (‘.$e->getFile().‘:‘.$e->getLine().")\n"; $msg .= $e->getTraceAsString()."\n"; $msg .= "Previous error:\n"; $msg .= $log."\n"; $msg .= ‘$_SERVER=‘.var_export($_SERVER,true); error_log($msg); //php中向服务器记录错误:参见 http://www.w3school.com.cn/php/php_ref_error.asp exit(1); } } }
第二个:
public function handleException($exception) { // disable error capturing to avoid recursive errors restore_error_handler(); restore_exception_handler(); $category=‘exception.‘.get_class($exception); if($exception instanceof CHttpException) $category.=‘.‘.$exception->statusCode; // php <5.2 doesn‘t support string conversion auto-magically $message=$exception->__toString(); if(isset($_SERVER[‘REQUEST_URI‘])) $message.="\nREQUEST_URI=".$_SERVER[‘REQUEST_URI‘]; if(isset($_SERVER[‘HTTP_REFERER‘])) $message.="\nHTTP_REFERER=".$_SERVER[‘HTTP_REFERER‘]; $message.="\n---"; Yii::log($message,CLogger::LEVEL_ERROR,$category); try { $event=new CExceptionEvent($this,$exception); $this->onException($event); if(!$event->handled) { // try an error handler if(($handler=$this->getErrorHandler())!==null) $handler->handle($event); else $this->displayException($exception); } } catch(Exception $e) { $this->displayException($e); } try { $this->end(1); } catch(Exception $e) { // use the most primitive way to log error $msg = get_class($e).‘: ‘.$e->getMessage().‘ (‘.$e->getFile().‘:‘.$e->getLine().")\n"; $msg .= $e->getTraceAsString()."\n"; $msg .= "Previous exception:\n"; $msg .= get_class($exception).‘: ‘.$exception->getMessage().‘ (‘.$exception->getFile().‘:‘.$exception->getLine().")\n"; $msg .= $exception->getTraceAsString()."\n"; $msg .= ‘$_SERVER=‘.var_export($_SERVER,true); error_log($msg); exit(1); } }
时间: 2024-10-17 09:48:17