Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
  • Loading branch information...
commit 6bf9363217137b6deb1dccb44ecc7d93875d49a3 1 parent 7becd58
@ceeram ceeram authored
View
3  app/Config/core.php
@@ -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.
*/
View
3  lib/Cake/Console/Templates/skel/Config/core.php
@@ -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.
*/
View
27 lib/Cake/Error/ErrorHandler.php
@@ -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);
@@ -160,6 +159,28 @@ protected static function _getMessage($exception) {
}
/**
+ * 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
* will log errors to CakeLog, when debug == 0.
View
41 lib/Cake/Test/Case/Error/ErrorHandlerTest.php
@@ -195,8 +195,6 @@ public function testHandleErrorLoggingTrace() {
* @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 @@ public function testHandleException() {
* @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 @@ public function testHandleExceptionLog() {
$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 @@ public function testLoadPluginHandler() {
* @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 @@ public function testHandleFatalErrorPage() {
* @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');
}
Please sign in to comment.
Something went wrong with that request. Please try again.