Skip to content

Commit

Permalink
File Log writer without newline. (#19941)
Browse files Browse the repository at this point in the history
* File Log writer without newline.

* Fix minor correction.

* Fix tests.

* Add more test.

* Add line to CHANGELOG.md.
  • Loading branch information
terabytesoftw authored Aug 30, 2023
1 parent a2ee220 commit 671fbef
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Yii Framework 2 Change Log
2.0.50 under development
------------------------

- no changes in this release.
- Bug #19940: File Log writer without newline (terabytesoftw)


2.0.49 August 29, 2023
Expand Down
4 changes: 2 additions & 2 deletions framework/log/FileTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ public function export()
if ($this->enableRotation && @filesize($this->logFile) > $this->maxFileSize * 1024) {
$this->rotateFiles();
}
$writeResult = @fwrite($fp, $trimmedText);
$writeResult = @fwrite($fp, $text);
if ($writeResult === false) {
$error = error_get_last();
throw new LogRuntimeException("Unable to export log through file ({$this->logFile})!: {$error['message']}");
}
$textSize = strlen($trimmedText);
$textSize = strlen($text);
if ($writeResult < $textSize) {
throw new LogRuntimeException("Unable to export whole log through file ({$this->logFile})! Wrote $writeResult out of $textSize bytes.");
}
Expand Down
14 changes: 13 additions & 1 deletion tests/framework/log/FileTargetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,19 @@ public function testLogEmptyStrings()
$logger->export();

$test = file($logFile);
$this->assertEquals("xxx", $test[0]);
$this->assertEquals("xxx\n", $test[0]);

$this->clearLogFile($logFile);

$logger = new CustomLogger();
$logger->logFile = $logFile;
$logger->messages = array_fill(0, 3, 'xxx');
$logger->export();

$test = file($logFile);
$this->assertEquals("xxx\n", $test[0]);
$this->assertEquals("xxx\n", $test[1]);
$this->assertEquals("xxx\n", $test[2]);

$this->clearLogFile($logFile);

Expand Down

1 comment on commit 671fbef

@Ryadnov
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.