mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Add option to skip exception types for logging.
add blacklisting for logging exceptions add test for skipping exception logging, and fix typo and cs add example to docblock in core.php for skipLog remove app_error.php skips, those are no longer used in 2.x use assertContains instead of assertRegExp check for instanceof, instead of matching names
This commit is contained in:
parent
7becd58237
commit
6bf9363217
4 changed files with 61 additions and 13 deletions
|
@ -70,6 +70,9 @@
|
|||
* - `renderer` - string - The class responsible for rendering uncaught exceptions. If you choose a custom class you
|
||||
* should place the file for that class in app/Lib/Error. This class needs to implement a render method.
|
||||
* - `log` - boolean - Should Exceptions be logged?
|
||||
* - `skipLog` - array - list of exceptions to skip for logging. Exceptions that
|
||||
* extend one of the listed exceptions will also be skipped for logging.
|
||||
* Example: `'skipLog' => array('NotFoundException', 'UnauthorizedException')`
|
||||
*
|
||||
* @see ErrorHandler for more information on exception handling and configuration.
|
||||
*/
|
||||
|
|
|
@ -70,6 +70,9 @@
|
|||
* - `renderer` - string - The class responsible for rendering uncaught exceptions. If you choose a custom class you
|
||||
* should place the file for that class in app/Lib/Error. This class needs to implement a render method.
|
||||
* - `log` - boolean - Should Exceptions be logged?
|
||||
* - `skipLog` - array - list of exceptions to skip for logging. Exceptions that
|
||||
* extend one of the listed exceptions will also be skipped for logging.
|
||||
* Example: `'skipLog' => array('NotFoundException', 'UnauthorizedException')`
|
||||
*
|
||||
* @see ErrorHandler for more information on exception handling and configuration.
|
||||
*/
|
||||
|
|
|
@ -110,9 +110,8 @@ class ErrorHandler {
|
|||
*/
|
||||
public static function handleException(Exception $exception) {
|
||||
$config = Configure::read('Exception');
|
||||
if (!empty($config['log'])) {
|
||||
CakeLog::write(LOG_ERR, self::_getMessage($exception));
|
||||
}
|
||||
self::_log($exception, $config);
|
||||
|
||||
$renderer = $config['renderer'];
|
||||
if ($renderer !== 'ExceptionRenderer') {
|
||||
list($plugin, $renderer) = pluginSplit($renderer, true);
|
||||
|
@ -159,6 +158,28 @@ class ErrorHandler {
|
|||
return $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles exception logging
|
||||
*
|
||||
* @param Exception $exception
|
||||
* @param array $config
|
||||
* @return boolean
|
||||
*/
|
||||
protected static function _log(Exception $exception, $config) {
|
||||
if (empty($config['log'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!empty($config['skipLog'])) {
|
||||
foreach ((array)$config['skipLog'] as $class) {
|
||||
if ($exception instanceof $class) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return CakeLog::write(LOG_ERR, self::_getMessage($exception));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set as the default error handler by CakePHP. Use Configure::write('Error.handler', $callback), to use your own
|
||||
* error handling methods. This function will use Debugger to display errors when debug > 0. And
|
||||
|
|
|
@ -195,8 +195,6 @@ class ErrorHandlerTest extends CakeTestCase {
|
|||
* @return void
|
||||
*/
|
||||
public function testHandleException() {
|
||||
$this->skipIf(file_exists(APP . 'app_error.php'), 'App error exists cannot run.');
|
||||
|
||||
$error = new NotFoundException('Kaboom!');
|
||||
ob_start();
|
||||
ErrorHandler::handleException($error);
|
||||
|
@ -210,8 +208,6 @@ class ErrorHandlerTest extends CakeTestCase {
|
|||
* @return void
|
||||
*/
|
||||
public function testHandleExceptionLog() {
|
||||
$this->skipIf(file_exists(APP . 'app_error.php'), 'App error exists cannot run.');
|
||||
|
||||
if (file_exists(LOGS . 'error.log')) {
|
||||
unlink(LOGS . 'error.log');
|
||||
}
|
||||
|
@ -224,8 +220,37 @@ class ErrorHandlerTest extends CakeTestCase {
|
|||
$this->assertRegExp('/Kaboom!/', $result, 'message missing.');
|
||||
|
||||
$log = file(LOGS . 'error.log');
|
||||
$this->assertRegExp('/\[NotFoundException\] Kaboom!/', $log[0], 'message missing.');
|
||||
$this->assertRegExp('/\#0.*ErrorHandlerTest->testHandleExceptionLog/', $log[2], 'Stack trace missing.');
|
||||
$this->assertContains('[NotFoundException] Kaboom!', $log[0], 'message missing.');
|
||||
$this->assertContains('ErrorHandlerTest->testHandleExceptionLog', $log[2], 'Stack trace missing.');
|
||||
}
|
||||
|
||||
/**
|
||||
* test handleException generating log.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testHandleExceptionLogSkipping() {
|
||||
if (file_exists(LOGS . 'error.log')) {
|
||||
unlink(LOGS . 'error.log');
|
||||
}
|
||||
Configure::write('Exception.log', true);
|
||||
Configure::write('Exception.skipLog', array('NotFoundException'));
|
||||
$notFound = new NotFoundException('Kaboom!');
|
||||
$forbidden = new ForbiddenException('Fooled you!');
|
||||
|
||||
ob_start();
|
||||
ErrorHandler::handleException($notFound);
|
||||
$result = ob_get_clean();
|
||||
$this->assertRegExp('/Kaboom!/', $result, 'message missing.');
|
||||
|
||||
ob_start();
|
||||
ErrorHandler::handleException($forbidden);
|
||||
$result = ob_get_clean();
|
||||
$this->assertRegExp('/Fooled you!/', $result, 'message missing.');
|
||||
|
||||
$log = file(LOGS . 'error.log');
|
||||
$this->assertNotContains('[NotFoundException] Kaboom!', $log[0], 'message should not be logged.');
|
||||
$this->assertContains('[ForbiddenException] Fooled you!', $log[0], 'message missing.');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -257,8 +282,6 @@ class ErrorHandlerTest extends CakeTestCase {
|
|||
* @return void
|
||||
*/
|
||||
public function testHandleFatalErrorPage() {
|
||||
$this->skipIf(file_exists(APP . 'app_error.php'), 'App error exists cannot run.');
|
||||
|
||||
$line = __LINE__;
|
||||
|
||||
ob_start();
|
||||
|
@ -286,8 +309,6 @@ class ErrorHandlerTest extends CakeTestCase {
|
|||
* @return void
|
||||
*/
|
||||
public function testHandleFatalErrorLog() {
|
||||
$this->skipIf(file_exists(APP . 'app_error.php'), 'App error exists cannot run.');
|
||||
|
||||
if (file_exists(LOGS . 'error.log')) {
|
||||
unlink(LOGS . 'error.log');
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue