Skip to content

Commit

Permalink
Fixed merging messages and file save when multiple log method was exe…
Browse files Browse the repository at this point in the history
…cuted
  • Loading branch information
chajr committed Aug 13, 2018
1 parent 16fb8d5 commit 02bc0cb
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
4 changes: 4 additions & 0 deletions doc/changelog.md
@@ -1,6 +1,10 @@
# Change Log
All notable changes to this project will be documented in this file.

## 0.7.2.0 - 2018-08-13
### Changed
* Fixed merging messages and file save when multiple log method was executed

## 0.7.1.1 - 2018-01-21
### Changed
* Fixed composer dependencies
Expand Down
21 changes: 15 additions & 6 deletions src/Log.php
Expand Up @@ -8,6 +8,8 @@
use Psr\Log\InvalidArgumentException;
use SimpleLog\Storage\StorageInterface;
use SimpleLog\Message\MessageInterface;
use SimpleLog\Storage\File;
use SimpleLog\Message\DefaultMessage;

class Log implements LogInterface, LoggerInterface
{
Expand All @@ -19,12 +21,12 @@ class Log implements LogInterface, LoggerInterface
protected $defaultParams = [
'log_path' => './log',
'level' => 'notice',
'storage' => \SimpleLog\Storage\File::class,
'message' => \SimpleLog\Message\DefaultMessage::class,
'storage' => File::class,
'message' => DefaultMessage::class,
];

/**
* @var \SimpleLog\Storage\StorageInterface
* @var StorageInterface
*/
protected $storage;

Expand All @@ -34,10 +36,15 @@ class Log implements LogInterface, LoggerInterface
protected $levels = [];

/**
* @var \SimpleLog\Message\MessageInterface
* @var MessageInterface
*/
protected $message;

/**
* @var MessageInterface
*/
protected $lastMessage;

/**
* @param array $params
* @throws \ReflectionException
Expand Down Expand Up @@ -71,7 +78,7 @@ public function makeLog($message, array $context = [])
* @param string $level
* @param string|array|object $message
* @param array $context
* @throws \Psr\Log\InvalidArgumentException
* @throws InvalidArgumentException
*/
public function log($level, $message, array $context = [])
{
Expand All @@ -84,6 +91,8 @@ public function log($level, $message, array $context = [])
->getMessage();

$this->storage->store($newMessage, $level);
$this->lastMessage = $this->message;
$this->reloadMessage();
}

/**
Expand Down Expand Up @@ -147,6 +156,6 @@ public function getOption($key = null)
*/
public function getLastMessage()
{
return $this->message->getMessage();
return $this->lastMessage->getMessage();
}
}
4 changes: 2 additions & 2 deletions src/Storage/File.php
Expand Up @@ -31,7 +31,7 @@ public function store($message, $level)
$logFile = $this->params['log_path'] . DIRECTORY_SEPARATOR . $level . '.log';

if (!file_exists($this->params['log_path'])) {
$bool = mkdir($this->params['log_path']);
$bool = @mkdir($this->params['log_path']);

if (!$bool) {
throw new LogException('Unable to create log directory: ' . $this->params['log_path']);
Expand All @@ -42,7 +42,7 @@ public function store($message, $level)
$flag = FILE_APPEND;
}

$bool = file_put_contents($logFile, $message, $flag);
$bool = @file_put_contents($logFile, $message, $flag | LOCK_EX);

if (!$bool) {
throw new LogException('Unable to save log file: ' . $logFile);
Expand Down
23 changes: 23 additions & 0 deletions tests/SimpleLogTest.php
Expand Up @@ -76,6 +76,29 @@ public function testCreateSimpleLogMessage()
//because of different time and date of creating log file, we remove first line with date
$this->assertEquals($this->getSampleContent(), substr($content, strpos($content, "\n") +1));
}

/**
* simple create log object and create log message in given directory
*/
public function testCreateMultipleSimpleLogMessage()
{
$log = new Log(['log_path' => $this->logPath]);

$this->assertFileNotExists($this->logPath . self::NOTICE_LOG_NAME);

$log->makeLog('Some log message');

$this->assertFileExists($this->logPath . self::NOTICE_LOG_NAME);

$log->makeLog('Some log message');

$content = file_get_contents($this->logPath . self::NOTICE_LOG_NAME);
//because of different time and date of creating log file, we remove first line with date
$this->assertEquals(
$this->getSampleContent() . $this->getSampleContent(),
preg_replace('#[\d]{4}-[\d]{2}-[\d]{2} - [\d]{2}:[\d]{2}:[\d]{2}\n#', '', $content)
);
}

public function testCreateSimpleLogWithOtherStorage()
{
Expand Down

0 comments on commit 02bc0cb

Please sign in to comment.