Skip to content
Permalink
Browse files

Adding CakeLogInterface and implementing it in core and test suite cl…

…asses. 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.
  • Loading branch information...
markstory committed Apr 24, 2010
1 parent 35c8f99 commit 33a29072026ee25d569be601485caf6072478193
@@ -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;
}
@@ -27,7 +27,7 @@
* @package cake
* @subpackage cake.cake.libs.log
*/
class FileLog {
class FileLog implements CakeLogInterface {
/**
* Path to save log files on.
@@ -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'));
}
@@ -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) {
@@ -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) {

0 comments on commit 33a2907

Please sign in to comment.
You can’t perform that action at this time.