码迷,mamicode.com
首页 > Web开发 > 详细

深入理解 PHP错误处理

时间:2016-01-13 12:30:47      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

首先,来看下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);
        }
    }

 

深入理解 PHP错误处理

标签:

原文地址:http://www.cnblogs.com/gaoqin-web/p/5126758.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!