Skip to content

Conversation

@olegbaturin
Copy link
Contributor

Q A
Is bugfix?
New feature?
Breaks BC?
Fixed issues

Call final flush from Logger::__destruct() method instead of register_shutdown_function() function

@codecov
Copy link

codecov bot commented Feb 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.77%. Comparing base (995fc0b) to head (ab64372).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master     #138      +/-   ##
============================================
+ Coverage     99.33%   99.77%   +0.44%     
- Complexity      162      163       +1     
============================================
  Files            11       11              
  Lines           451      449       -2     
============================================
  Hits            448      448              
+ Misses            3        1       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@samdark samdark left a comment

Choose a reason for hiding this comment

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

Good catch. I've verified that in all PHP versions we support, the destructor will be called after all shutdown functions in all cases we handle. 👍

During the testing I've found one edge case that our logger doesn't handle (and probably can't handle at all). That's when there's exit() or an exception thrown in __destruct() or any service created before the logger. But since it's almost impossible considering the framework init sequence, that's fine.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request changes how the Logger performs its final flush operation, moving from using register_shutdown_function() to implementing a __destruct() method. This changes when the final flush occurs - from guaranteed execution at script shutdown to execution when the Logger object is destroyed (goes out of scope).

Changes:

  • Removed register_shutdown_function() usage from Logger constructor
  • Added __destruct() method that calls flush(true) when Logger is destroyed
  • Updated test expectations to account for additional flush call from destructor

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/Logger.php Removed register_shutdown_function import and shutdown function registration; added __destruct() method calling flush(true)
tests/LoggerTest.php Updated mock expectations to account for destructor flush - changed from once() to exactly(2) and exactly(2) to exactly(3)
CHANGELOG.md Added entry documenting the change from shutdown function to destructor approach
Comments suppressed due to low confidence (1)

tests/LoggerTest.php:402

  • The test expects exactly 3 calls to the collect method, but withConsecutive only defines 2 argument sets. A third argument set needs to be added to match the expected call from __destruct(), which should be an empty message array with final=true, similar to the second call in testDispatchWithSuccessTargetCollect.
            ->expects($this->exactly(3))
            ->method('collect')
            ->withConsecutive(
                [$this->equalTo([$message]), $this->equalTo(true)],
                [
                    $this->callback(function ($messages) use ($target1, $exception) {
                        $message = $messages[0] ?? null;
                        $text = 'Unable to send log via ' . $target1::class . ': RuntimeException: some error';
                        return ((is_countable($messages) ? count($messages) : 0) === 1 && $message instanceof Message)
                            && $message->level() === LogLevel::WARNING
                            && $message->message() === $text
                            && is_float($message->context('time'))
                            && $message->context('exception') === $exception;
                    }),
                    $this->equalTo(true),
                ],
            );

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@samdark samdark merged commit 27f5093 into yiisoft:master Feb 5, 2026
31 checks passed
@samdark
Copy link
Member

samdark commented Feb 5, 2026

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants