From 33a29072026ee25d569be601485caf6072478193 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Fri, 23 Apr 2010 22:31:21 -0400 Subject: [PATCH] Adding CakeLogInterface and implementing it in core and test suite classes. The new interface is used for method detection instead of a hard method check. Making CakeLog throw exceptions instead of trigger errors when things go wrong. --- cake/libs/cake_log.php | 46 ++++++++++++------- cake/libs/log/file_log.php | 2 +- cake/tests/cases/libs/cake_log.test.php | 14 ++++-- cake/tests/test_app/libs/log/test_app_log.php | 2 +- .../test_plugin/libs/log/test_plugin_log.php | 2 +- 5 files changed, 42 insertions(+), 24 deletions(-) diff --git a/cake/libs/cake_log.php b/cake/libs/cake_log.php index e2b12131278..062e7e053ab 100644 --- a/cake/libs/cake_log.php +++ b/cake/libs/cake_log.php @@ -37,6 +37,24 @@ define('LOG_INFO', 6); } +/** + * CakeLogStreamInterface is the interface that should be implemented + * by all classes that are going to be used as Log streams. + * + * @package cake.libs + */ +interface CakeLogInterface { +/** + * Write method to handle writes being made to the Logger + * + * @param string $type + * @param string $message + * @return void + * @author Mark Story + */ + public function write($type, $message); +} + /** * Logs messages to configured Log adapters. One or more adapters can be configured * using CakeLogs's methods. If you don't configure any adapters, and write to the logs @@ -80,19 +98,23 @@ class CakeLog { * @param string $key The keyname for this logger, used to revmoe the logger later. * @param array $config Array of configuration information for the logger * @return boolean success of configuration. + * @throws Exception * @static */ function config($key, $config) { if (empty($config['engine'])) { - trigger_error(__('Missing logger classname'), E_USER_WARNING); - return false; - } - $className = self::_getLogger($config['engine']); - if (!$className) { - return false; + throw new Exception(__('Missing logger classname')); } + $loggerName = $config['engine']; unset($config['engine']); - self::$_streams[$key] = new $className($config); + $className = self::_getLogger($loggerName); + $logger = new $className($config); + if (!$logger instanceof CakeLogInterface) { + throw new Exception(sprintf( + __('logger class %s does not implement a write method.'), $loggerName + )); + } + self::$_streams[$key] = $logger; return true; } @@ -114,15 +136,7 @@ protected static function _getLogger($loggerName) { } } if (!class_exists($loggerName)) { - trigger_error(sprintf(__('Could not load logger class %s'), $loggerName), E_USER_WARNING); - return false; - } - if (!is_callable(array($loggerName, 'write'))) { - trigger_error( - sprintf(__('logger class %s does not implement a write method.'), $loggerName), - E_USER_WARNING - ); - return false; + throw new Exception(sprintf(__('Could not load class %s'), $loggerName)); } return $loggerName; } diff --git a/cake/libs/log/file_log.php b/cake/libs/log/file_log.php index 133000e1827..e191fd67477 100644 --- a/cake/libs/log/file_log.php +++ b/cake/libs/log/file_log.php @@ -27,7 +27,7 @@ * @package cake * @subpackage cake.cake.libs.log */ -class FileLog { +class FileLog implements CakeLogInterface { /** * Path to save log files on. diff --git a/cake/tests/cases/libs/cake_log.test.php b/cake/tests/cases/libs/cake_log.test.php index 115a468a694..2ed6ce66fc5 100644 --- a/cake/tests/cases/libs/cake_log.test.php +++ b/cake/tests/cases/libs/cake_log.test.php @@ -72,13 +72,17 @@ function testImportingLoggers() { * @return void */ function testImportingLoggerFailure() { - $this->expectError('Missing logger classname'); + $this->expectException(); CakeLog::config('fail', array()); + } - $this->expectError('Could not load logger class born to fail'); - CakeLog::config('fail', array('engine' => 'born to fail')); - - $this->expectError('logger class stdClass does not implement a write method.'); +/** + * test that loggers have to implement the correct interface. + * + * @return void + */ + function testNotImplementingInterface() { + $this->expectException(); CakeLog::config('fail', array('engine' => 'stdClass')); } diff --git a/cake/tests/test_app/libs/log/test_app_log.php b/cake/tests/test_app/libs/log/test_app_log.php index 58cb9b5bab2..191e772de60 100644 --- a/cake/tests/test_app/libs/log/test_app_log.php +++ b/cake/tests/test_app/libs/log/test_app_log.php @@ -17,7 +17,7 @@ * @since CakePHP(tm) v 1.3 * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License */ -class TestAppLog { +class TestAppLog implements CakeLogInterface { function write($type, $message) { diff --git a/cake/tests/test_app/plugins/test_plugin/libs/log/test_plugin_log.php b/cake/tests/test_app/plugins/test_plugin/libs/log/test_plugin_log.php index dde74f61a89..b1a5e533a55 100644 --- a/cake/tests/test_app/plugins/test_plugin/libs/log/test_plugin_log.php +++ b/cake/tests/test_app/plugins/test_plugin/libs/log/test_plugin_log.php @@ -17,7 +17,7 @@ * @since CakePHP(tm) v 1.3 * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License */ -class TestPluginLog { +class TestPluginLog implements CakeLogInterface { function write($type, $message) {