Browse Source

Using log instead of write in log engines and updating tests

Jose Lorenzo Rodriguez 11 years ago
parent
commit
0634efb592

+ 2 - 3
src/Log/Engine/ConsoleLog.php

@@ -82,11 +82,10 @@ class ConsoleLog extends BaseLog {
  *
  * @param string $level The severity level of log you are making.
  * @param string $message The message you want to log.
- * @param string|array $scope The scope(s) a log message is being created in.
- *    See Cake\Log\Log::config() for more information on logging scopes.
+ * @param array $context Additional information about the logged message
  * @return bool success of write.
  */
-	public function write($level, $message, $scope = []) {
+	public function log($level, $message, array $context = []) {
 		$output = date('Y-m-d H:i:s') . ' ' . ucfirst($level) . ': ' . $message . "\n";
 		return $this->_output->write(sprintf('<%s>%s</%s>', $level, $output, $level), false);
 	}

+ 6 - 4
src/Log/Engine/FileLog.php

@@ -16,6 +16,7 @@ namespace Cake\Log\Engine;
 
 use Cake\Core\Configure;
 use Cake\Log\Engine\BaseLog;
+use Cake\Utility\String;
 
 /**
  * File Storage stream for Logging. Writes logs to different files
@@ -100,7 +101,7 @@ class FileLog extends BaseLog {
 			if (is_numeric($this->_config['size'])) {
 				$this->_size = (int)$this->_config['size'];
 			} else {
-				$this->_size = CakeNumber::fromReadableSize($this->_config['size']);
+				$this->_size = String::parseFileSize($this->_config['size']);
 			}
 		}
 	}
@@ -111,11 +112,10 @@ class FileLog extends BaseLog {
  * @param string $level The severity level of the message being written.
  *    See Cake\Log\Log::$_levels for list of possible levels.
  * @param string $message The message you want to log.
- * @param string|array $scope The scope(s) a log message is being created in.
- *    See Cake\Log\Log::config() for more information on logging scopes.
+ * @param array $context Additional information about the logged message
  * @return bool success of write.
  */
-	public function write($level, $message, $scope = []) {
+	public function log($level, $message, array $context = []) {
 		$output = date('Y-m-d H:i:s') . ' ' . ucfirst($level) . ': ' . $message . "\n";
 		$filename = $this->_getFilename($level);
 		if (!empty($this->_size)) {
@@ -131,6 +131,7 @@ class FileLog extends BaseLog {
 		$exists = file_exists($pathname);
 		$result = file_put_contents($pathname, $output, FILE_APPEND);
 		static $selfError = false;
+
 		if (!$selfError && !$exists && !chmod($pathname, (int)$mask)) {
 			$selfError = true;
 			trigger_error(vsprintf(
@@ -138,6 +139,7 @@ class FileLog extends BaseLog {
 				array($mask, $pathname)), E_USER_WARNING);
 			$selfError = false;
 		}
+
 		return $result;
 	}
 

+ 2 - 3
src/Log/Engine/SyslogLog.php

@@ -89,11 +89,10 @@ class SyslogLog extends BaseLog {
  *
  * @param string $level The severity level of log you are making.
  * @param string $message The message you want to log.
- * @param string|array $scope The scope(s) a log message is being created in.
- *    See Cake\Log\Log::config() for more information on logging scopes.
+ * @param array $context Additional information about the logged message
  * @return bool success of write.
  */
-	public function write($level, $message, $scope = []) {
+	public function log($level, $message, array $context = []) {
 		if (!$this->_open) {
 			$config = $this->_config;
 			$this->_open($config['prefix'], $config['flag'], $config['facility']);

+ 4 - 4
tests/TestCase/Log/Engine/ConsoleLogTest.php

@@ -27,7 +27,7 @@ class ConsoleLogTest extends TestCase {
 /**
  * Test writing to ConsoleOutput
  */
-	public function testConsoleOutputWrites() {
+	public function testConsoleOutputlogs() {
 		$output = $this->getMock('Cake\Console\ConsoleOutput');
 
 		$output->expects($this->at(0))
@@ -41,7 +41,7 @@ class ConsoleLogTest extends TestCase {
 		$log = new ConsoleLog([
 			'stream' => $output
 		]);
-		$log->write('error', 'oh noes');
+		$log->log('error', 'oh noes');
 	}
 
 /**
@@ -49,12 +49,12 @@ class ConsoleLogTest extends TestCase {
  *
  * @return void
  */
-	public function testWriteToFileStream() {
+	public function testlogToFileStream() {
 		$filename = tempnam(sys_get_temp_dir(), 'cake_log_test');
 		$log = new ConsoleLog([
 			'stream' => $filename
 		]);
-		$log->write('error', 'oh noes');
+		$log->log('error', 'oh noes');
 		$fh = fopen($filename, 'r');
 		$line = fgets($fh);
 		$this->assertContains('Error: oh noes', $line);

+ 13 - 13
tests/TestCase/Log/Engine/FileLogTest.php

@@ -34,19 +34,19 @@ class FileLogTest extends TestCase {
 		$this->_deleteLogs(LOGS);
 
 		$log = new FileLog();
-		$log->write('warning', 'Test warning');
+		$log->log('warning', 'Test warning');
 		$this->assertTrue(file_exists(LOGS . 'error.log'));
 
 		$result = file_get_contents(LOGS . 'error.log');
 		$this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning/', $result);
 
-		$log->write('debug', 'Test warning');
+		$log->log('debug', 'Test warning');
 		$this->assertTrue(file_exists(LOGS . 'debug.log'));
 
 		$result = file_get_contents(LOGS . 'debug.log');
 		$this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Debug: Test warning/', $result);
 
-		$log->write('random', 'Test warning');
+		$log->log('random', 'Test warning');
 		$this->assertTrue(file_exists(LOGS . 'random.log'));
 
 		$result = file_get_contents(LOGS . 'random.log');
@@ -54,7 +54,7 @@ class FileLogTest extends TestCase {
 	}
 
 /**
- * test using the path setting to write logs in other places.
+ * test using the path setting to log logs in other places.
  *
  * @return void
  */
@@ -63,7 +63,7 @@ class FileLogTest extends TestCase {
 		$this->_deleteLogs($path);
 
 		$log = new FileLog(compact('path'));
-		$log->write('warning', 'Test warning');
+		$log->log('warning', 'Test warning');
 		$this->assertTrue(file_exists($path . 'error.log'));
 	}
 
@@ -82,7 +82,7 @@ class FileLogTest extends TestCase {
 			'size' => 35,
 			'rotate' => 2
 		));
-		$log->write('warning', 'Test warning one');
+		$log->log('warning', 'Test warning one');
 		$this->assertTrue(file_exists($path . 'error.log'));
 
 		$result = file_get_contents($path . 'error.log');
@@ -90,7 +90,7 @@ class FileLogTest extends TestCase {
 		$this->assertEquals(0, count(glob($path . 'error.log.*')));
 
 		clearstatcache();
-		$log->write('warning', 'Test warning second');
+		$log->log('warning', 'Test warning second');
 
 		$files = glob($path . 'error.log.*');
 		$this->assertEquals(1, count($files));
@@ -101,7 +101,7 @@ class FileLogTest extends TestCase {
 
 		sleep(1);
 		clearstatcache();
-		$log->write('warning', 'Test warning third');
+		$log->log('warning', 'Test warning third');
 
 		$result = file_get_contents($path . 'error.log');
 		$this->assertRegExp('/Warning: Test warning third/', $result);
@@ -119,7 +119,7 @@ class FileLogTest extends TestCase {
 
 		sleep(1);
 		clearstatcache();
-		$log->write('warning', 'Test warning fourth');
+		$log->log('warning', 'Test warning fourth');
 
 		// rotate count reached so file count should not increase
 		$files = glob($path . 'error.log.*');
@@ -141,7 +141,7 @@ class FileLogTest extends TestCase {
 			'rotate' => 0
 		));
 		file_put_contents($path . 'debug.log.0000000000', "The oldest log file with over 35 bytes.\n");
-		$log->write('debug', 'Test debug');
+		$log->log('debug', 'Test debug');
 		$this->assertTrue(file_exists($path . 'debug.log'));
 
 		$result = file_get_contents($path . 'debug.log');
@@ -159,21 +159,21 @@ class FileLogTest extends TestCase {
 		$this->_deleteLogs($path);
 
 		$log = new FileLog(array('path' => $path, 'mask' => 0666));
-		$log->write('warning', 'Test warning one');
+		$log->log('warning', 'Test warning one');
 		$result = substr(sprintf('%o', fileperms($path . 'error.log')), -4);
 		$expected = '0666';
 		$this->assertEquals($expected, $result);
 		unlink($path . 'error.log');
 
 		$log = new FileLog(array('path' => $path, 'mask' => 0644));
-		$log->write('warning', 'Test warning two');
+		$log->log('warning', 'Test warning two');
 		$result = substr(sprintf('%o', fileperms($path . 'error.log')), -4);
 		$expected = '0644';
 		$this->assertEquals($expected, $result);
 		unlink($path . 'error.log');
 
 		$log = new FileLog(array('path' => $path, 'mask' => 0640));
-		$log->write('warning', 'Test warning three');
+		$log->log('warning', 'Test warning three');
 		$result = substr(sprintf('%o', fileperms($path . 'error.log')), -4);
 		$expected = '0640';
 		$this->assertEquals($expected, $result);

+ 4 - 4
tests/TestCase/Log/Engine/SyslogLogTest.php

@@ -29,7 +29,7 @@ class SyslogLogTest extends TestCase {
 	public function testOpenLog() {
 		$log = $this->getMock('Cake\Log\Engine\SyslogLog', array('_open', '_write'));
 		$log->expects($this->once())->method('_open')->with('', LOG_ODELAY, LOG_USER);
-		$log->write('debug', 'message');
+		$log->log('debug', 'message');
 
 		$log = $this->getMock('Cake\Log\Engine\SyslogLog', array('_open', '_write'));
 		$log->config(array(
@@ -40,7 +40,7 @@ class SyslogLogTest extends TestCase {
 		));
 		$log->expects($this->once())->method('_open')
 			->with('thing', LOG_NDELAY, LOG_MAIL);
-		$log->write('debug', 'message');
+		$log->log('debug', 'message');
 	}
 
 /**
@@ -52,7 +52,7 @@ class SyslogLogTest extends TestCase {
 	public function testWriteOneLine($type, $expected) {
 		$log = $this->getMock('Cake\Log\Engine\SyslogLog', array('_open', '_write'));
 		$log->expects($this->once())->method('_write')->with($expected, $type . ': Foo');
-		$log->write($type, 'Foo');
+		$log->log($type, 'Foo');
 	}
 
 /**
@@ -65,7 +65,7 @@ class SyslogLogTest extends TestCase {
 		$log->expects($this->at(1))->method('_write')->with(LOG_DEBUG, 'debug: Foo');
 		$log->expects($this->at(2))->method('_write')->with(LOG_DEBUG, 'debug: Bar');
 		$log->expects($this->exactly(2))->method('_write');
-		$log->write('debug', "Foo\nBar");
+		$log->log('debug', "Foo\nBar");
 	}
 
 /**

+ 1 - 1
tests/TestCase/Log/LogTest.php

@@ -440,7 +440,7 @@ class LogTest extends TestCase {
 		$this->assertNull($engine->passedScope);
 
 		Log::write('debug', 'test message', 'foo');
-		$this->assertEquals('foo', $engine->passedScope);
+		$this->assertEquals(['foo'], $engine->passedScope);
 
 		Log::write('debug', 'test message', ['foo', 'bar']);
 		$this->assertEquals(['foo', 'bar'], $engine->passedScope);

+ 2 - 2
tests/TestCase/Log/LogTraitTest.php

@@ -37,11 +37,11 @@ class LogTraitTest extends TestCase {
 	public function testLog() {
 		$mock = $this->getMock('Cake\Log\LogInterface');
 		$mock->expects($this->at(0))
-			->method('write')
+			->method('log')
 			->with('error', 'Testing');
 
 		$mock->expects($this->at(1))
-			->method('write')
+			->method('log')
 			->with('debug', print_r(array(1, 2), true));
 
 		Log::config('trait_test', ['engine' => $mock]);

+ 3 - 2
tests/test_app/Plugin/TestPlugin/src/Log/Engine/TestPluginLog.php

@@ -15,14 +15,15 @@
 namespace TestPlugin\Log\Engine;
 
 use Cake\Log\LogInterface;
+use Psr\Log\AbstractLogger;
 
 /**
  * Test Suite Test Plugin Logging stream class.
  *
  */
-class TestPluginLog implements LogInterface {
+class TestPluginLog extends AbstractLogger {
 
-	public function write($level, $message, $scope = []) {
+	public function log($level, $message, array $context = []) {
 	}
 
 }

+ 2 - 2
tests/test_app/TestApp/Log/Engine/TestAppLog.php

@@ -26,8 +26,8 @@ class TestAppLog extends BaseLog {
 
 	public $passedScope = null;
 
-	public function write($level, $message, $scope = []) {
-		$this->passedScope = $scope;
+	public function log($level, $message, array $context = []) {
+		$this->passedScope = $context;
 	}
 
 }