Implementing CakeLog::config() adding simple tests for config().

This commit is contained in:
mark_story 2009-11-05 19:16:46 -05:00
parent 98f0e370a1
commit d23f2f4604
3 changed files with 95 additions and 14 deletions

View file

@ -70,7 +70,7 @@ class CakeLog {
* @return void * @return void
* @static * @static
**/ **/
function getInstance() { function &getInstance() {
static $instance = array(); static $instance = array();
if (!isset($instance[0])) { if (!isset($instance[0])) {
$instance[0] = new CakeLog(); $instance[0] = new CakeLog();
@ -78,6 +78,54 @@ class CakeLog {
return $instance[0]; return $instance[0];
} }
/**
* Configure and add a new logging stream to CakeLog
* You can use add loggers from app/libs use app.loggername, or any plugin/libs using plugin.loggername
*
* @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 void
**/
function config($key, $config) {
if (empty($config['engine'])) {
trigger_error(__('Missing logger classname', true), E_USER_WARNING);
return false;
}
$className = CakeLog::_getLogger($config['engine']);
if (!$className) {
return false;
}
unset($config['engine']);
$self = CakeLog::getInstance();
$self->_streams[$key] = new $className($config);
}
/**
* Attempts to import a logger class from the various paths it could be on.
* Checks that the logger class implements a write method as well.
*
* @return mixed boolean false on any failures, string of classname to use if search was successful.
**/
function _getLogger($loggerName) {
$plugin = null;
if (strpos($loggerName, '.') !== false) {
list($plugin, $loggerName) = explode('.', $loggerName);
}
if (!class_exists($loggerName)) {
trigger_error(sprintf(__('Could not load logger class %s', true), $loggerName), E_USER_WARNING);
return false;
}
if (!method_exists($loggerName, 'write')) {
trigger_error(
sprintf(__('logger class %s does not implement a write method.', true), $loggerName),
E_USER_WARNING
);
return false;
}
return $loggerName;
}
/** /**
* Returns the keynames of the currently active streams * Returns the keynames of the currently active streams
* *
@ -97,7 +145,7 @@ class CakeLog {
* @return void * @return void
* @static * @static
**/ **/
function removeStream($streamName) { function remove($streamName) {
$self = CakeLog::getInstance(); $self = CakeLog::getInstance();
unset($self->_streams[$streamName]); unset($self->_streams[$streamName]);
} }
@ -122,10 +170,10 @@ class CakeLog {
* @return void * @return void
**/ **/
function _autoConfig() { function _autoConfig() {
if (!class_exists('FileLogger')) { if (!class_exists('FileLog')) {
require LIBS . 'log' . DS . 'file.php'; require LIBS . 'log' . DS . 'file_log.php';
} }
$this->_streams['default'] = array('FileLogger', 'write'); $this->_streams['default'] = new FileLog(array('path' => LOGS));
} }
/** /**
@ -160,8 +208,10 @@ class CakeLog {
if (empty($self->_streams)) { if (empty($self->_streams)) {
$self->_autoConfig(); $self->_autoConfig();
} }
foreach ($self->_streams as $key => $callable) { $keys = array_keys($self->_streams);
call_user_func($callable, $type, $message); foreach ($keys as $key) {
$logger =& $self->_streams[$key];
$logger->write($type, $message);
} }
} }

View file

@ -26,7 +26,7 @@
* @package cake * @package cake
* @subpackage cake.cake.libs.log * @subpackage cake.cake.libs.log
*/ */
class FileLogger { class FileLog {
/** /**
* Implements writing to log files. * Implements writing to log files.
* *

View file

@ -21,6 +21,7 @@
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/ */
App::import('Core', 'Log'); App::import('Core', 'Log');
require_once LIBS . 'log' . DS . 'file_log.php';
/** /**
* CakeLogTest class * CakeLogTest class
@ -30,6 +31,18 @@ App::import('Core', 'Log');
*/ */
class CakeLogTest extends CakeTestCase { class CakeLogTest extends CakeTestCase {
/**
* Start test callback, clears all streams enabled.
*
* @return void
**/
function startTest() {
$streams = CakeLog::streams();
foreach ($streams as $stream) {
CakeLog::remove($stream);
}
}
/** /**
* Test that CakeLog autoconfigures itself to use a FileLogger with the LOGS dir. * Test that CakeLog autoconfigures itself to use a FileLogger with the LOGS dir.
* When no streams are there. * When no streams are there.
@ -37,17 +50,35 @@ class CakeLogTest extends CakeTestCase {
* @return void * @return void
**/ **/
function testAutoConfig() { function testAutoConfig() {
$streams = CakeLog::streams();
foreach ($streams as $stream) {
CakeLog::removeStream($stream);
}
@unlink(LOGS . 'error.log'); @unlink(LOGS . 'error.log');
CakeLog::write(LOG_WARNING, 'Test warning'); CakeLog::write(LOG_WARNING, 'Test warning');
$this->assertTrue(file_exists(LOGS . 'error.log')); $this->assertTrue(file_exists(LOGS . 'error.log'));
$result = CakeLog::streams(); $result = CakeLog::streams();
$this->assertEqual($result, array('default')); $this->assertEqual($result, array('default'));
unlink(LOGS . 'error.log');
}
/**
* test configuring log streams
*
* @return void
**/
function testConfig() {
CakeLog::config('file', array(
'engine' => 'FileLog',
'path' => LOGS
));
$result = CakeLog::streams();
$this->assertEqual($result, array('file'));
@unlink(LOGS . 'error.log');
CakeLog::write(LOG_WARNING, 'Test warning');
$this->assertTrue(file_exists(LOGS . 'error.log'));
$result = file_get_contents(LOGS . 'error.log');
$this->assertPattern('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning/', $result);
unlink(LOGS . 'error.log');
} }
/** /**
@ -86,7 +117,7 @@ class CakeLogTest extends CakeTestCase {
$result = file(LOGS . 'debug.log'); $result = file(LOGS . 'debug.log');
$this->assertEqual(count($result), 1); $this->assertEqual(count($result), 1);
$this->assertPattern( $this->assertPattern(
'/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} Notice: Notice \(8\): Undefined variable: out in \[.+ line \d{2}\]$/', '/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} Notice: Notice \(8\): Undefined variable: out in \[.+ line \d+\]$/',
$result[0] $result[0]
); );
@unlink(LOGS . 'debug.log'); @unlink(LOGS . 'debug.log');