From abafc3bd25fb782466698348812c2902f9feca43 Mon Sep 17 00:00:00 2001 From: Oleg Baturin Date: Wed, 4 Feb 2026 18:12:10 +0700 Subject: [PATCH 1/3] fix final flush --- src/Logger.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Logger.php b/src/Logger.php index e3decbe6..d62504d3 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -19,7 +19,6 @@ use function in_array; use function is_string; use function microtime; -use function register_shutdown_function; use function sprintf; /** @@ -87,14 +86,11 @@ public function __construct( ) { $this->setTargets($targets); $this->contextProvider = $contextProvider ?? new SystemContextProvider(); + } - register_shutdown_function(function () { - // make regular flush before other shutdown functions, which allows session data collection and so on - $this->flush(); - // make sure log entries written by shutdown functions are also flushed - // ensure "flush()" is called last when there are multiple shutdown functions - register_shutdown_function([$this, 'flush'], true); - }); + public function __destruct() + { + $this->flush(true); } /** @@ -146,6 +142,10 @@ public function log(mixed $level, string|Stringable $message, array $context = [ */ public function flush(bool $final = false): void { + if (empty($this->messages)) { + return; + } + $messages = $this->messages; // https://github.com/yiisoft/yii2/issues/5619 // new messages could be logged while the existing ones are being handled by targets From 688088904959b1a1b9accaceb5fd5eb68b455110 Mon Sep 17 00:00:00 2001 From: Oleg Baturin Date: Wed, 4 Feb 2026 18:20:23 +0700 Subject: [PATCH 2/3] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ad62563..163809d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - Chg #130, #133: Changed `Message::parse()` to conform to PSR-3 (@technicated, @vjik) - Enh #135: Add validation for `$traceLevel` in `SystemContextProvider` to ensure values are greater than or equal to zero (@rekmixa) - Enh #137: Explicitly import classes, functions, and constants in "use" section (@mspirkov) +- Enh #138: Moved the final log flush from `register_shutdown_function()` to the `Logger::__destruct()` method (@olegbaturin) ## 2.2.0 December 13, 2025 From ab643721168ab409635e8db389b081ef1f3729e2 Mon Sep 17 00:00:00 2001 From: Oleg Baturin Date: Wed, 4 Feb 2026 18:52:59 +0700 Subject: [PATCH 3/3] fix tests --- src/Logger.php | 4 ---- tests/LoggerTest.php | 10 +++++----- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/Logger.php b/src/Logger.php index d62504d3..54b619f0 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -142,10 +142,6 @@ public function log(mixed $level, string|Stringable $message, array $context = [ */ public function flush(bool $final = false): void { - if (empty($this->messages)) { - return; - } - $messages = $this->messages; // https://github.com/yiisoft/yii2/issues/5619 // new messages could be logged while the existing ones are being handled by targets diff --git a/tests/LoggerTest.php b/tests/LoggerTest.php index 297c1164..c9f541e5 100644 --- a/tests/LoggerTest.php +++ b/tests/LoggerTest.php @@ -349,11 +349,11 @@ public function testDispatchWithSuccessTargetCollect(): void ->getMockForAbstractClass(); $target - ->expects($this->once()) + ->expects($this->exactly(2)) ->method('collect') - ->with( - $this->equalTo([$message]), - $this->equalTo(true), + ->withConsecutive( + [$this->equalTo([$message]), $this->equalTo(true)], + [$this->equalTo([]), $this->equalTo(true)], ); $logger = new Logger(['fakeTarget' => $target]); @@ -383,7 +383,7 @@ public function testDispatchWithFakeTarget2ThrowExceptionWhenCollect(): void ->getMockForAbstractClass(); $target1 - ->expects($this->exactly(2)) + ->expects($this->exactly(3)) ->method('collect') ->withConsecutive( [$this->equalTo([$message]), $this->equalTo(true)],