Skip to content

Commit 33a2907

Browse files
committed
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.
1 parent 35c8f99 commit 33a2907

File tree

5 files changed

+42
-24
lines changed

5 files changed

+42
-24
lines changed

cake/libs/cake_log.php

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@
3737
define('LOG_INFO', 6);
3838
}
3939

40+
/**
41+
* CakeLogStreamInterface is the interface that should be implemented
42+
* by all classes that are going to be used as Log streams.
43+
*
44+
* @package cake.libs
45+
*/
46+
interface CakeLogInterface {
47+
/**
48+
* Write method to handle writes being made to the Logger
49+
*
50+
* @param string $type
51+
* @param string $message
52+
* @return void
53+
* @author Mark Story
54+
*/
55+
public function write($type, $message);
56+
}
57+
4058
/**
4159
* Logs messages to configured Log adapters. One or more adapters can be configured
4260
* using CakeLogs's methods. If you don't configure any adapters, and write to the logs
@@ -80,19 +98,23 @@ class CakeLog {
8098
* @param string $key The keyname for this logger, used to revmoe the logger later.
8199
* @param array $config Array of configuration information for the logger
82100
* @return boolean success of configuration.
101+
* @throws Exception
83102
* @static
84103
*/
85104
function config($key, $config) {
86105
if (empty($config['engine'])) {
87-
trigger_error(__('Missing logger classname'), E_USER_WARNING);
88-
return false;
89-
}
90-
$className = self::_getLogger($config['engine']);
91-
if (!$className) {
92-
return false;
106+
throw new Exception(__('Missing logger classname'));
93107
}
108+
$loggerName = $config['engine'];
94109
unset($config['engine']);
95-
self::$_streams[$key] = new $className($config);
110+
$className = self::_getLogger($loggerName);
111+
$logger = new $className($config);
112+
if (!$logger instanceof CakeLogInterface) {
113+
throw new Exception(sprintf(
114+
__('logger class %s does not implement a write method.'), $loggerName
115+
));
116+
}
117+
self::$_streams[$key] = $logger;
96118
return true;
97119
}
98120

@@ -114,15 +136,7 @@ protected static function _getLogger($loggerName) {
114136
}
115137
}
116138
if (!class_exists($loggerName)) {
117-
trigger_error(sprintf(__('Could not load logger class %s'), $loggerName), E_USER_WARNING);
118-
return false;
119-
}
120-
if (!is_callable(array($loggerName, 'write'))) {
121-
trigger_error(
122-
sprintf(__('logger class %s does not implement a write method.'), $loggerName),
123-
E_USER_WARNING
124-
);
125-
return false;
139+
throw new Exception(sprintf(__('Could not load class %s'), $loggerName));
126140
}
127141
return $loggerName;
128142
}

cake/libs/log/file_log.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* @package cake
2828
* @subpackage cake.cake.libs.log
2929
*/
30-
class FileLog {
30+
class FileLog implements CakeLogInterface {
3131

3232
/**
3333
* Path to save log files on.

cake/tests/cases/libs/cake_log.test.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,17 @@ function testImportingLoggers() {
7272
* @return void
7373
*/
7474
function testImportingLoggerFailure() {
75-
$this->expectError('Missing logger classname');
75+
$this->expectException();
7676
CakeLog::config('fail', array());
77+
}
7778

78-
$this->expectError('Could not load logger class born to fail');
79-
CakeLog::config('fail', array('engine' => 'born to fail'));
80-
81-
$this->expectError('logger class stdClass does not implement a write method.');
79+
/**
80+
* test that loggers have to implement the correct interface.
81+
*
82+
* @return void
83+
*/
84+
function testNotImplementingInterface() {
85+
$this->expectException();
8286
CakeLog::config('fail', array('engine' => 'stdClass'));
8387
}
8488

cake/tests/test_app/libs/log/test_app_log.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* @since CakePHP(tm) v 1.3
1818
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
1919
*/
20-
class TestAppLog {
20+
class TestAppLog implements CakeLogInterface {
2121

2222
function write($type, $message) {
2323

cake/tests/test_app/plugins/test_plugin/libs/log/test_plugin_log.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* @since CakePHP(tm) v 1.3
1818
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
1919
*/
20-
class TestPluginLog {
20+
class TestPluginLog implements CakeLogInterface {
2121

2222
function write($type, $message) {
2323

0 commit comments

Comments
 (0)