Adding exception logging configuration setting. Allows uncaught exceptions to be logged.

Implementing logging, and adding a test case.
This commit is contained in:
mark_story 2010-11-14 23:33:46 -05:00
parent af4447d55d
commit f2f3f13c75
3 changed files with 40 additions and 5 deletions

View file

@ -58,9 +58,16 @@
* ErrorHandler::handleException() is used. It will display a HTML page for the exception, and * ErrorHandler::handleException() is used. It will display a HTML page for the exception, and
* while debug > 0, framework errors like Missing Controller will be displayed. When debug = 0, * while debug > 0, framework errors like Missing Controller will be displayed. When debug = 0,
* framework errors will be coerced into generic HTTP errors. * framework errors will be coerced into generic HTTP errors.
*
* Options:
*
* - `handler` - callback - The callback to handle exceptions. You can set this to any callback type,
* including anonymous functions.
* - `log` - boolean - Should Exceptions be logged?
*/ */
Configure::write('Exception', array( Configure::write('Exception', array(
'handler' => 'ErrorHandler::handleException' 'handler' => 'ErrorHandler::handleException',
'log' => true
)); ));
/** /**

View file

@ -72,6 +72,12 @@ class ErrorHandler {
$AppError = new AppError($exception); $AppError = new AppError($exception);
return $AppError->render(); return $AppError->render();
} }
if (Configure::read('Exception.log')) {
if (!class_exists('CakeLog')) {
require LIBS . 'cake_log.php';
}
CakeLog::write(LOG_ERR, '[' . get_class($exception) . '] ' . $exception->getMessage());
}
$error = new ExceptionRenderer($exception); $error = new ExceptionRenderer($exception);
$error->render(); $error->render();
} }

View file

@ -18,7 +18,7 @@
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/ */
App::import('Core', 'ErrorHandler'); App::import('Core', array('ErrorHandler', 'Controller', 'Router'));
/** /**
* ErrorHandlerTest class * ErrorHandlerTest class
@ -145,9 +145,7 @@ class ErrorHandlerTest extends CakeTestCase {
if ($this->skipIf(file_exists(APP . 'app_error.php'), 'App error exists cannot run.')) { if ($this->skipIf(file_exists(APP . 'app_error.php'), 'App error exists cannot run.')) {
return; return;
} }
if ($this->skipIf(PHP_SAPI == 'cli', 'This integration test can not be run in cli.')) {
return;
}
$error = new NotFoundException('Kaboom!'); $error = new NotFoundException('Kaboom!');
ob_start(); ob_start();
ErrorHandler::handleException($error); ErrorHandler::handleException($error);
@ -155,4 +153,28 @@ class ErrorHandlerTest extends CakeTestCase {
$this->assertPattern('/Kaboom!/', $result, 'message missing.'); $this->assertPattern('/Kaboom!/', $result, 'message missing.');
} }
/**
* test handleException generating a page.
*
* @return void
*/
function testHandleExceptionLog() {
if ($this->skipIf(file_exists(APP . 'app_error.php'), 'App error exists cannot run.')) {
return;
}
if (file_exists(LOGS . 'error.log')) {
unlink(LOGS . 'error.log');
}
Configure::write('Exception.log', true);
$error = new NotFoundException('Kaboom!');
ob_start();
ErrorHandler::handleException($error);
$result = ob_get_clean();
$this->assertPattern('/Kaboom!/', $result, 'message missing.');
$log = file(LOGS . 'error.log');
$this->assertPattern('/\[NotFoundException\] Kaboom!/', $log[0], 'message missing.');
}
} }