From 671fbefe8457568ea1b83750f8a98a60bc62d9cd Mon Sep 17 00:00:00 2001 From: Wilmer Arambula <42547589+terabytesoftw@users.noreply.github.com> Date: Wed, 30 Aug 2023 08:14:12 -0400 Subject: [PATCH] File Log writer without newline. (#19941) * File Log writer without newline. * Fix minor correction. * Fix tests. * Add more test. * Add line to CHANGELOG.md. --- framework/CHANGELOG.md | 2 +- framework/log/FileTarget.php | 4 ++-- tests/framework/log/FileTargetTest.php | 14 +++++++++++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index b1f82b066f6..a96d0eff96e 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -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 diff --git a/framework/log/FileTarget.php b/framework/log/FileTarget.php index e56909c32f1..d816f73a8db 100644 --- a/framework/log/FileTarget.php +++ b/framework/log/FileTarget.php @@ -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."); } diff --git a/tests/framework/log/FileTargetTest.php b/tests/framework/log/FileTargetTest.php index fb712580573..4f6eaf4ae75 100644 --- a/tests/framework/log/FileTargetTest.php +++ b/tests/framework/log/FileTargetTest.php @@ -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);