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 diff --git a/src/Logger.php b/src/Logger.php index e3decbe6..54b619f0 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); } /** 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)],