Permalink
Browse files

Refactoring FileLog

Adding test case for FileLog
  • Loading branch information...
1 parent d23f2f4 commit 01bb9e5957605a2cea8fc0b58d9c5baf2eef2d84 @markstory markstory committed Nov 6, 2009
Showing with 99 additions and 16 deletions.
  1. +32 −16 cake/libs/log/file_log.php
  2. +67 −0 cake/tests/cases/libs/log/file_log.test.php
@@ -27,30 +27,46 @@
* @subpackage cake.cake.libs.log
*/
class FileLog {
+
/**
- * Implements writing to log files.
+ * Path to save log files on.
+ *
+ *
+ * @var string
+ **/
+ var $_path = null;
+
+/**
+ * Constructs a new File Logger.
+ *
+ * Options
+ *
+ * - `path` the path to save logs on.
*
+ * @param array $options Options for the FileLog, see above.
* @return void
**/
+ function FileLog($options = array()) {
+ $options += array('path' => LOGS);
+ $this->_path = $options['path'];
+ }
+
+/**
+ * Implements writing to log files.
+ *
+ * @param string $type The type of log you are making.
+ * @param string $message The message you want to log.
+ * @return boolean success of write.
+ **/
function write($type, $message) {
- $levels = array(
- LOG_WARNING => 'warning',
- LOG_NOTICE => 'notice',
- LOG_INFO => 'info',
- LOG_DEBUG => 'debug',
- LOG_ERR => 'error',
- LOG_ERROR => 'error'
- );
+ $debugTypes = array('notice', 'info', 'debug');
- if (is_int($type) && isset($levels[$type])) {
- $type = $levels[$type];
- }
if ($type == 'error' || $type == 'warning') {
- $filename = LOGS . 'error.log';
- } elseif (in_array($type, $levels)) {
- $filename = LOGS . 'debug.log';
+ $filename = $this->_path . 'error.log';
+ } elseif (in_array($type, $debugTypes)) {
+ $filename = $this->_path . 'debug.log';
} else {
- $filename = LOGS . $type . '.log';
+ $filename = $this->_path . $type . '.log';
}
$output = date('Y-m-d H:i:s') . ' ' . ucfirst($type) . ': ' . $message . "\n";
$log = new File($filename, true);
@@ -0,0 +1,67 @@
+<?php
+/**
+ * FileLogTest file
+ *
+ * Long description for file
+ *
+ * PHP versions 4 and 5
+ *
+ * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
+ * Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
+ *
+ * Licensed under The Open Group Test Suite License
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @filesource
+ * @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
+ * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
+ * @package cake
+ * @subpackage cake.tests.cases.libs.log
+ * @since CakePHP(tm) v 1.3
+ * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
+ */
+require_once LIBS . 'log' . DS . 'file_log.php';
+
+/**
+ * CakeLogTest class
+ *
+ * @package cake
+ * @subpackage cake.tests.cases.libs
+ */
+class FileLogTest extends CakeTestCase {
+
+/**
+ * testLogFileWriting method
+ *
+ * @access public
+ * @return void
+ */
+ function testLogFileWriting() {
+ @unlink(LOGS . 'error.log');
+ $log =& new FileLog();
+ $log->write('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');
+
+ @unlink(LOGS . 'debug.log');
+ $log->write('debug', 'Test warning');
+ $this->assertTrue(file_exists(LOGS . 'debug.log'));
+
+ $result = file_get_contents(LOGS . 'debug.log');
+ $this->assertPattern('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Debug: Test warning/', $result);
+ unlink(LOGS . 'debug.log');
+
+ @unlink(LOGS . 'random.log');
+ $log->write('random', 'Test warning');
+ $this->assertTrue(file_exists(LOGS . 'random.log'));
+
+ $result = file_get_contents(LOGS . 'random.log');
+ $this->assertPattern('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Random: Test warning/', $result);
+ unlink(LOGS . 'random.log');
+ }
+
+}
+?>

0 comments on commit 01bb9e5

Please sign in to comment.