Skip to content

Commit

Permalink
Adding CakeLogInterface and implementing it in core and test suite cl…
Browse files Browse the repository at this point in the history
…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 33a2907
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 24 deletions.
46 changes: 30 additions & 16 deletions cake/libs/cake_log.php
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion cake/libs/log/file_log.php
Expand Up @@ -27,7 +27,7 @@
* @package cake
* @subpackage cake.cake.libs.log
*/
class FileLog {
class FileLog implements CakeLogInterface {

/**
* Path to save log files on.
Expand Down
14 changes: 9 additions & 5 deletions cake/tests/cases/libs/cake_log.test.php
Expand Up @@ -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'));
}

Expand Down
2 changes: 1 addition & 1 deletion cake/tests/test_app/libs/log/test_app_log.php
Expand Up @@ -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) {

Expand Down
Expand Up @@ -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) {

Expand Down

0 comments on commit 33a2907

Please sign in to comment.