From f52bc48576968e2ba407061a8169ad958677f7cb Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Sat, 13 Apr 2013 23:16:11 -0400 Subject: [PATCH] use error_log to log fatal errors. --- framework/base/Application.php | 137 ++++++++++++++++------------------------- 1 file changed, 54 insertions(+), 83 deletions(-) diff --git a/framework/base/Application.php b/framework/base/Application.php index 1053210..b9f01d7 100644 --- a/framework/base/Application.php +++ b/framework/base/Application.php @@ -13,36 +13,6 @@ use yii\helpers\FileHelper; /** * Application is the base class for all application classes. * - * An application serves as the global context that the user request - * is being processed. It manages a set of application components that - * provide specific functionalities to the whole application. - * - * The core application components provided by Application are the following: - * - * - * Application will undergo the following life cycles when processing a user request: - *
    - *
  1. load application configuration;
  2. - *
  3. set up class autoloader and error handling;
  4. - *
  5. load static application components;
  6. - *
  7. {@link beforeRequest}: preprocess the user request; `beforeRequest` event raised.
  8. - *
  9. {@link processRequest}: process the user request;
  10. - *
  11. {@link afterRequest}: postprocess the user request; `afterRequest` event raised.
  12. - *
- * - * Starting from lifecycle 3, if a PHP error or an uncaught exception occurs, - * the application will switch to its error handling logic and jump to step 6 afterwards. - * * @author Qiang Xue * @since 2.0 */ @@ -157,30 +127,6 @@ class Application extends Module } /** - * Handles fatal PHP errors - */ - public function handleFatalError() - { - if (YII_ENABLE_ERROR_HANDLER) { - $error = error_get_last(); - - if (ErrorException::isFatalError($error)) { - unset($this->_memoryReserve); - $exception = new ErrorException($error['message'], $error['type'], $error['type'], $error['file'], $error['line']); - $this->logException($exception); - - if (($handler = $this->getErrorHandler()) !== null) { - @$handler->handle($exception); - } else { - $this->renderException($exception); - } - - exit(1); - } - } - } - - /** * Runs the application. * This is the main entrance of an application. * @return integer the exit status (0 means normal, non-zero values mean abnormal) @@ -384,6 +330,45 @@ class Application extends Module } /** + * Handles uncaught PHP exceptions. + * + * This method is implemented as a PHP exception handler. It requires + * that constant YII_ENABLE_ERROR_HANDLER be defined true. + * + * @param \Exception $exception exception that is not caught + */ + public function handleException($exception) + { + // disable error capturing to avoid recursive errors while handling exceptions + restore_error_handler(); + restore_exception_handler(); + + try { + $this->logException($exception); + + if (($handler = $this->getErrorHandler()) !== null) { + $handler->handle($exception); + } else { + $this->renderException($exception); + } + + $this->end(1); + + } catch (\Exception $e) { + // exception could be thrown in end() or ErrorHandler::handle() + $msg = (string)$e; + $msg .= "\nPrevious exception:\n"; + $msg .= (string)$exception; + if (YII_DEBUG) { + echo $msg; + } + $msg .= "\n\$_SERVER = " . var_export($_SERVER, true); + error_log($msg); + exit(1); + } + } + + /** * Handles PHP execution errors such as warnings, notices. * * This method is used as a PHP error handler. It will simply raise an `ErrorException`. @@ -414,41 +399,27 @@ class Application extends Module } /** - * Handles uncaught PHP exceptions. - * - * This method is implemented as a PHP exception handler. It requires - * that constant YII_ENABLE_ERROR_HANDLER be defined true. - * - * @param \Exception $exception exception that is not caught + * Handles fatal PHP errors */ - public function handleException($exception) + public function handleFatalError() { - // disable error capturing to avoid recursive errors while handling exceptions - restore_error_handler(); - restore_exception_handler(); - - try { - $this->logException($exception); + if (YII_ENABLE_ERROR_HANDLER) { + $error = error_get_last(); - if (($handler = $this->getErrorHandler()) !== null) { - $handler->handle($exception); - } else { - $this->renderException($exception); - } + if (ErrorException::isFatalError($error)) { + unset($this->_memoryReserve); + $exception = new ErrorException($error['message'], $error['type'], $error['type'], $error['file'], $error['line']); + // use error_log because it's too late to use Yii log + error_log($exception); - $this->end(1); + if (($handler = $this->getErrorHandler()) !== null) { + @$handler->handle($exception); + } else { + $this->renderException($exception); + } - } catch (\Exception $e) { - // exception could be thrown in end() or ErrorHandler::handle() - $msg = (string)$e; - $msg .= "\nPrevious exception:\n"; - $msg .= (string)$exception; - if (YII_DEBUG) { - echo $msg; + exit(1); } - $msg .= "\n\$_SERVER = " . var_export($_SERVER, true); - error_log($msg); - exit(1); } }