Browse Source

use error_log to log fatal errors.

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
f52bc48576
  1. 137
      framework/base/Application.php

137
framework/base/Application.php

@ -13,36 +13,6 @@ use yii\helpers\FileHelper;
/** /**
* Application is the base class for all application classes. * 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:
* <ul>
* <li>{@link getErrorHandler errorHandler}: handles PHP errors and
* uncaught exceptions. This application component is dynamically loaded when needed.</li>
* <li>{@link getSecurityManager securityManager}: provides security-related
* services, such as hashing, encryption. This application component is dynamically
* loaded when needed.</li>
* <li>{@link getStatePersister statePersister}: provides global state
* persistence method. This application component is dynamically loaded when needed.</li>
* <li>{@link getCache cache}: provides caching feature. This application component is
* disabled by default.</li>
* </ul>
*
* Application will undergo the following life cycles when processing a user request:
* <ol>
* <li>load application configuration;</li>
* <li>set up class autoloader and error handling;</li>
* <li>load static application components;</li>
* <li>{@link beforeRequest}: preprocess the user request; `beforeRequest` event raised.</li>
* <li>{@link processRequest}: process the user request;</li>
* <li>{@link afterRequest}: postprocess the user request; `afterRequest` event raised.</li>
* </ol>
*
* 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 <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @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. * Runs the application.
* This is the main entrance of an application. * This is the main entrance of an application.
* @return integer the exit status (0 means normal, non-zero values mean abnormal) * @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. * Handles PHP execution errors such as warnings, notices.
* *
* This method is used as a PHP error handler. It will simply raise an `ErrorException`. * 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. * Handles fatal PHP errors
*
* 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) public function handleFatalError()
{ {
// disable error capturing to avoid recursive errors while handling exceptions if (YII_ENABLE_ERROR_HANDLER) {
restore_error_handler(); $error = error_get_last();
restore_exception_handler();
try {
$this->logException($exception);
if (($handler = $this->getErrorHandler()) !== null) { if (ErrorException::isFatalError($error)) {
$handler->handle($exception); unset($this->_memoryReserve);
} else { $exception = new ErrorException($error['message'], $error['type'], $error['type'], $error['file'], $error['line']);
$this->renderException($exception); // 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) { exit(1);
// 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);
} }
} }

Loading…
Cancel
Save