From c9b801b96d96b246c76d17759431ce654ad3b73e Mon Sep 17 00:00:00 2001 From: Rachman Chavik Date: Tue, 15 May 2012 10:38:46 +0700 Subject: [PATCH] making CakeLog to work with bogus $type and $scope closes #2881 --- lib/Cake/Log/CakeLog.php | 9 +++-- lib/Cake/Test/Case/Log/CakeLogTest.php | 48 +++++++++++++++++++++----- 2 files changed, 45 insertions(+), 12 deletions(-) diff --git a/lib/Cake/Log/CakeLog.php b/lib/Cake/Log/CakeLog.php index e38192b46..af958f48a 100644 --- a/lib/Cake/Log/CakeLog.php +++ b/lib/Cake/Log/CakeLog.php @@ -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; } diff --git a/lib/Cake/Test/Case/Log/CakeLogTest.php b/lib/Cake/Test/Case/Log/CakeLogTest.php index de3ad9668..7e0f05abb 100644 --- a/lib/Cake/Test/Case/Log/CakeLogTest.php +++ b/lib/Cake/Test/Case/Log/CakeLogTest.php @@ -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(); } }