From be9bcfd4b5da178ab142898b35b388a45fffc28c Mon Sep 17 00:00:00 2001 From: Rachman Chavik Date: Thu, 10 May 2012 22:52:52 +0700 Subject: [PATCH] adding convenience methods for CakeLog --- lib/Cake/Log/CakeLog.php | 45 +++++++++++++++++++++ lib/Cake/Test/Case/Log/CakeLogTest.php | 54 ++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) diff --git a/lib/Cake/Log/CakeLog.php b/lib/Cake/Log/CakeLog.php index 5ccaaa546..450781c69 100644 --- a/lib/Cake/Log/CakeLog.php +++ b/lib/Cake/Log/CakeLog.php @@ -296,4 +296,49 @@ class CakeLog { return true; } +/** + * Convenience method to log error messages + * + * @return boolean Success + */ + public static function error($message) { + return self::write(LOG_ERROR, $message); + } + +/** + * Convenience method to log warning messages + * + * @return boolean Success + */ + public static function warning($message) { + return self::write(LOG_WARNING, $message); + } + +/** + * Convenience method to log notice messages + * + * @return boolean Success + */ + public static function notice($message) { + return self::write(LOG_NOTICE, $message); + } + +/** + * Convenience method to log debug messages + * + * @return boolean Success + */ + public static function debug($message) { + return self::write(LOG_DEBUG, $message); + } + +/** + * Convenience method to log info messages + * + * @return boolean Success + */ + public static function info($message) { + return self::write(LOG_INFO, $message); + } + } diff --git a/lib/Cake/Test/Case/Log/CakeLogTest.php b/lib/Cake/Test/Case/Log/CakeLogTest.php index 7eec36f4b..518bdc94f 100644 --- a/lib/Cake/Test/Case/Log/CakeLogTest.php +++ b/lib/Cake/Test/Case/Log/CakeLogTest.php @@ -429,4 +429,58 @@ class CakeLogTest extends CakeTestCase { CakeLog::drop('shops'); } +/** + * test convenience methods + */ + public function testConvenienceMethods() { + @unlink(LOGS . 'error.log'); + @unlink(LOGS . 'debug.log'); + + CakeLog::config('debug', array( + 'engine' => 'FileLog', + 'types' => array('notice', 'info', 'debug'), + 'file' => 'debug', + )); + CakeLog::config('error', array( + 'engine' => 'FileLog', + 'types' => array('error', 'warning'), + 'file' => 'error', + )); + + $testMessage = 'error message'; + CakeLog::error($testMessage); + $contents = file_get_contents(LOGS . 'error.log'); + $this->assertContains($testMessage, $contents); + $this->assertFalse(file_exists(LOGS . 'debug.log')); + $this->_deleteLogs(); + + $testMessage = 'warning message'; + CakeLog::warning($testMessage); + $contents = file_get_contents(LOGS . 'error.log'); + $this->assertContains($testMessage, $contents); + $this->assertFalse(file_exists(LOGS . 'debug.log')); + $this->_deleteLogs(); + + $testMessage = 'info message'; + CakeLog::info($testMessage); + $contents = file_get_contents(LOGS . 'debug.log'); + $this->assertContains($testMessage, $contents); + $this->assertFalse(file_exists(LOGS . 'error.log')); + $this->_deleteLogs(); + + $testMessage = 'debug message'; + CakeLog::debug($testMessage); + $contents = file_get_contents(LOGS . 'debug.log'); + $this->assertContains($testMessage, $contents); + $this->assertFalse(file_exists(LOGS . 'error.log')); + $this->_deleteLogs(); + + $testMessage = 'notice message'; + CakeLog::notice($testMessage); + $contents = file_get_contents(LOGS . 'debug.log'); + $this->assertContains($testMessage, $contents); + $this->assertFalse(file_exists(LOGS . 'error.log')); + $this->_deleteLogs(); + } + }