Skip to content

Commit

Permalink
Using log instead of write in log engines and updating tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Sep 19, 2014
1 parent ff1161d commit 0634efb
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 38 deletions.
5 changes: 2 additions & 3 deletions src/Log/Engine/ConsoleLog.php
Expand Up @@ -82,11 +82,10 @@ public function __construct(array $config = array()) {
*
* @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);
}
Expand Down
10 changes: 6 additions & 4 deletions src/Log/Engine/FileLog.php
Expand Up @@ -16,6 +16,7 @@

use Cake\Core\Configure;
use Cake\Log\Engine\BaseLog;
use Cake\Utility\String;

/**
* File Storage stream for Logging. Writes logs to different files
Expand Down Expand Up @@ -100,7 +101,7 @@ public function __construct(array $config = []) {
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']);
}
}
}
Expand All @@ -111,11 +112,10 @@ public function __construct(array $config = []) {
* @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)) {
Expand All @@ -131,13 +131,15 @@ public function write($level, $message, $scope = []) {
$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(
'Could not apply permission mask "%s" on log file "%s"',
array($mask, $pathname)), E_USER_WARNING);
$selfError = false;
}

return $result;
}

Expand Down
5 changes: 2 additions & 3 deletions src/Log/Engine/SyslogLog.php
Expand Up @@ -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']);
Expand Down
8 changes: 4 additions & 4 deletions tests/TestCase/Log/Engine/ConsoleLogTest.php
Expand Up @@ -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))
Expand All @@ -41,20 +41,20 @@ public function testConsoleOutputWrites() {
$log = new ConsoleLog([
'stream' => $output
]);
$log->write('error', 'oh noes');
$log->log('error', 'oh noes');
}

/**
* Test writing to a file stream
*
* @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);
Expand Down
26 changes: 13 additions & 13 deletions tests/TestCase/Log/Engine/FileLogTest.php
Expand Up @@ -34,27 +34,27 @@ public function testLogFileWriting() {
$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');
$this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Random: Test warning/', $result);
}

/**
* test using the path setting to write logs in other places.
* test using the path setting to log logs in other places.
*
* @return void
*/
Expand All @@ -63,7 +63,7 @@ public function testPathSetting() {
$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'));
}

Expand All @@ -82,15 +82,15 @@ public function testRotation() {
'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');
$this->assertRegExp('/Warning: Test warning one/', $result);
$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));
Expand All @@ -101,7 +101,7 @@ public function testRotation() {

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);
Expand All @@ -119,7 +119,7 @@ public function testRotation() {

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.*');
Expand All @@ -141,7 +141,7 @@ public function testRotation() {
'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');
Expand All @@ -159,21 +159,21 @@ public function testMaskSetting() {
$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);
Expand Down
8 changes: 4 additions & 4 deletions tests/TestCase/Log/Engine/SyslogLogTest.php
Expand Up @@ -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(
Expand All @@ -40,7 +40,7 @@ public function testOpenLog() {
));
$log->expects($this->once())->method('_open')
->with('thing', LOG_NDELAY, LOG_MAIL);
$log->write('debug', 'message');
$log->log('debug', 'message');
}

/**
Expand All @@ -52,7 +52,7 @@ public function testOpenLog() {
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');
}

/**
Expand All @@ -65,7 +65,7 @@ public function testWriteMultiLine() {
$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");
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Log/LogTest.php
Expand Up @@ -440,7 +440,7 @@ public function testPassingScopeToEngine() {
$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);
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Log/LogTraitTest.php
Expand Up @@ -37,11 +37,11 @@ public function tearDown() {
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]);
Expand Down
Expand Up @@ -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 = []) {
}

}
4 changes: 2 additions & 2 deletions tests/test_app/TestApp/Log/Engine/TestAppLog.php
Expand Up @@ -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;
}

}

0 comments on commit 0634efb

Please sign in to comment.