Skip to content

Commit

Permalink
Merge pull request #14361 from mtak3/log_date_format
Browse files Browse the repository at this point in the history
Allow changing log date format
  • Loading branch information
dereuromark committed Apr 6, 2020
2 parents fa12f03 + 69e779b commit 89b8791
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/Log/Engine/BaseLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ abstract class BaseLog extends AbstractLogger
protected $_defaultConfig = [
'levels' => [],
'scopes' => [],
'dateFormat' => 'Y-m-d H:i:s',
];

/**
Expand Down Expand Up @@ -159,4 +160,16 @@ protected function _format(string $message, array $context = []): string

return str_replace(array_keys($replacements), $replacements, $message);
}

/**
* Returns date formatted according to given `dateFormat` option format.
*
* This function affects `FileLog` or` ConsoleLog` datetime information format.
*
* @return string
*/
protected function _getFormattedDate()
{
return date($this->_config['dateFormat']);
}
}
4 changes: 3 additions & 1 deletion src/Log/Engine/ConsoleLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class ConsoleLog extends BaseLog
'levels' => null,
'scopes' => [],
'outputAs' => null,
'dateFormat' => 'Y-m-d H:i:s',
];

/**
Expand All @@ -52,6 +53,7 @@ class ConsoleLog extends BaseLog
* - `scopes` string or array, scopes the engine is interested in
* - `stream` the path to save logs on.
* - `outputAs` integer or ConsoleOutput::[RAW|PLAIN|COLOR]
* - `dateFormat` PHP date() format.
*
* @param array $config Options for the FileLog, see above.
* @throws \InvalidArgumentException
Expand Down Expand Up @@ -86,7 +88,7 @@ public function __construct(array $config = [])
public function log($level, $message, array $context = [])
{
$message = $this->_format($message, $context);
$output = date('Y-m-d H:i:s') . ' ' . ucfirst($level) . ': ' . $message;
$output = $this->_getFormattedDate() . ' ' . ucfirst($level) . ': ' . $message;

$this->_output->write(sprintf('<%s>%s</%s>', $level, $output, $level));
}
Expand Down
4 changes: 3 additions & 1 deletion src/Log/Engine/FileLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class FileLog extends BaseLog
* If value is 0, old versions are removed rather then rotated.
* - `mask` A mask is applied when log files are created. Left empty no chmod
* is made.
* - `dateFormat` PHP date() format.
*
* @var array
*/
Expand All @@ -52,6 +53,7 @@ class FileLog extends BaseLog
'rotate' => 10,
'size' => 10485760, // 10MB
'mask' => null,
'dateFormat' => 'Y-m-d H:i:s',
];

/**
Expand Down Expand Up @@ -117,7 +119,7 @@ public function __construct(array $config = [])
public function log($level, $message, array $context = []): void
{
$message = $this->_format($message, $context);
$output = date('Y-m-d H:i:s') . ' ' . ucfirst($level) . ': ' . $message . "\n";
$output = $this->_getFormattedDate() . ' ' . ucfirst($level) . ': ' . $message . "\n";
$filename = $this->_getFilename($level);
if ($this->_size) {
$this->_rotateFile($filename);
Expand Down
19 changes: 19 additions & 0 deletions tests/TestCase/Log/Engine/ConsoleLogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function testlogToFileStream()
$fh = fopen($filename, 'r');
$line = fgets($fh);
$this->assertStringContainsString('Error: oh noes', $line);
$this->assertRegExp('/2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Error: oh noes/', $line);
}

/**
Expand All @@ -76,4 +77,22 @@ public function testDefaultOutputAs()
]);
$this->assertEquals(ConsoleOutput::RAW, $log->getConfig('outputAs'));
}

/**
* test dateFormat option
*
* @return void
*/
public function testDateFormat()
{
$filename = tempnam(sys_get_temp_dir(), 'cake_log_test');
$log = new ConsoleLog([
'stream' => $filename,
'dateFormat' => 'c',
]);
$log->log('error', 'oh noes');
$fh = fopen($filename, 'r');
$line = fgets($fh);
$this->assertRegExp('/2[0-9]{3}-[0-9]+-[0-9]+T[0-9]+:[0-9]+:[0-9]+\+\d{2}:\d{2} Error: oh noes/', $line);
}
}
19 changes: 19 additions & 0 deletions tests/TestCase/Log/Engine/FileLogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,23 @@ protected function _deleteLogs($dir)
unlink($file);
}
}

/**
* test dateFormat option
*
* @return void
*/
public function testDateFormat()
{
$this->_deleteLogs(LOGS);

// original 'Y-m-d H:i:s' format test was testLogFileWriting() method

// 'c': ISO 8601 date (added in PHP 5)
$log = new FileLog(['path' => LOGS, 'dateFormat' => 'c']);
$log->log('warning', 'Test warning');

$result = file_get_contents(LOGS . 'error.log');
$this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+T[0-9]+:[0-9]+:[0-9]+\+\d{2}:\d{2} Warning: Test warning/', $result);
}
}

0 comments on commit 89b8791

Please sign in to comment.