making CakeLog to work with bogus $type and $scope

closes #2881
This commit is contained in:
Rachman Chavik 2012-05-15 10:38:46 +07:00
parent a37bdba8fe
commit c9b801b96d
2 changed files with 45 additions and 12 deletions

View file

@ -416,9 +416,7 @@ class CakeLog {
if (is_string($type) && empty($scope) && !in_array($type, self::$_levels)) {
$scope = $type;
}
if (!self::$_Collection->attached()) {
self::_autoConfig();
}
$logged = false;
foreach (self::$_Collection->enabled() as $streamName) {
$logger = self::$_Collection->{$streamName};
$types = null;
@ -440,8 +438,13 @@ class CakeLog {
}
if (empty($types) || in_array($type, $types) || in_array($type, $scopes) && $inScope) {
$logger->write($type, $message);
$logged = true;
}
}
if (!$logged) {
self::_autoConfig();
self::stream('default')->write($type, $message);
}
return true;
}

View file

@ -318,21 +318,22 @@ class CakeLogTest extends CakeTestCase {
if (file_exists(LOGS . 'debug.log')) {
unlink(LOGS . 'debug.log');
}
if (file_exists(LOGS . 'bogus.log')) {
unlink(LOGS . 'bogus.log');
}
if (file_exists(LOGS . 'spam.log')) {
unlink(LOGS . 'spam.log');
}
if (file_exists(LOGS . 'eggs.log')) {
unlink(LOGS . 'eggs.log');
}
}
/**
* test backward compatible scoped logging
*/
public function testScopedLoggingBC() {
if (file_exists(LOGS . 'shops.log')) {
unlink(LOGS . 'shops.log');
}
if (file_exists(LOGS . 'error.log')) {
unlink(LOGS . 'error.log');
}
if (file_exists(LOGS . 'debug.log')) {
unlink(LOGS . 'debug.log');
}
$this->_deleteLogs();
$this->_resetLogConfig();
CakeLog::config('shops', array(
@ -446,6 +447,33 @@ class CakeLogTest extends CakeTestCase {
CakeLog::drop('shops');
}
/**
* test bogus type and scope
*
*/
public function testBogusTypeAndScope() {
$this->_resetLogConfig();
$this->_deleteLogs();
CakeLog::write('bogus', 'bogus message');
$this->assertTrue(file_exists(LOGS . 'bogus.log'));
$this->assertFalse(file_exists(LOGS . 'error.log'));
$this->assertFalse(file_exists(LOGS . 'debug.log'));
$this->_deleteLogs();
CakeLog::write('bogus', 'bogus message', 'bogus');
$this->assertTrue(file_exists(LOGS . 'bogus.log'));
$this->assertFalse(file_exists(LOGS . 'error.log'));
$this->assertFalse(file_exists(LOGS . 'debug.log'));
$this->_deleteLogs();
CakeLog::write('error', 'bogus message', 'bogus');
$this->assertFalse(file_exists(LOGS . 'bogus.log'));
$this->assertTrue(file_exists(LOGS . 'error.log'));
$this->assertFalse(file_exists(LOGS . 'debug.log'));
$this->_deleteLogs();
}
/**
* test scoped logging with convenience methods
*/
@ -641,6 +669,8 @@ class CakeLogTest extends CakeTestCase {
CakeLog::drop('spam');
CakeLog::drop('eggs');
$this->_deleteLogs();
}
}