Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Test Logger monolog compatibility #35596

Closed
wants to merge 5 commits into from

Conversation

convenient
Copy link
Contributor

@convenient convenient commented Jun 9, 2022

Not sure if this is the preferred approach, but one of our automated suites which builds magento with our module caught this today.

System info

  • Magento 2.4.4
  • PHP 8.1

To reproduce

  1. Get a setup of 2.4.4 installed
  2. See that in the constraints allow for an install of monolog/monolog at 2.7.0
  3. Try to run integration tests (https://devdocs.magento.com/guides/v2.4/test/integration/integration_test_execution.html)

The error

Currently we get this error because the method signatures are different

PHP Fatal error:  Declaration of Magento\TestFramework\ErrorLog\Logger::addRecord(int $level, string $message, array $context = []): bool must be compatible with Monolog\Logger::addRecord(int $level, string $message, array $context = [], ?Monolog\DateTimeImmutable $datetime = null): bool in dev/tests/integration/framework/Magento/TestFramework/ErrorLog/Logger.php on line 69

Root cause

This version of monolog/monolog was tagged about an hour ago https://github.com/Seldaek/monolog/releases/tag/2.7.0

See this entry

Added $datetime parameter to Logger::addRecord as low level API to allow logging into the past or future (Seldaek/monolog#1682)

Which goes along with this commit
Seldaek/monolog@0ddba73#diff-1e7fd545cec457de96f5ed6bd7249ba091cd9e699b4057db15ff1e2e0364025bR297

This changes the method signature of addRecord like so

-    public function addRecord(int|Level $level, string $message, array $context = []): bool
+    public function addRecord(int|Level $level, string $message, array $context = [], DateTimeImmutable $datetime = null): bool 

See the magento core logger is defined like this

public function addRecord(
int $level,
string $message,
array $context = []
): bool {

The method signature needs to be updated to match monolog/monolog definition

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • README.md files for modified modules are updated and included in the pull request if any README.md predefined sections require an update
  • All automated tests passed successfully (all builds are green)

# System info
- Magento 2.4.4
- PHP 8.1

# To reproduce
1. Get a setup of 2.4.4 installed
2. See that in the constraints allow for an install of `monolog/logger` at `2.7.0`
3. Try to run integration tests (https://devdocs.magento.com/guides/v2.4/test/integration/integration_test_execution.html)

# The error
Currently we get this error because the method signatures are different

```
PHP Fatal error:  Declaration of Magento\TestFramework\ErrorLog\Logger::addRecord(int $level, string $message, array $context = []): bool must be compatible with Monolog\Logger::addRecord(int $level, string $message, array $context = [], ?Monolog\DateTimeImmutable $datetime = null): bool in dev/tests/integration/framework/Magento/TestFramework/ErrorLog/Logger.php on line 69
```

# Root cause
This version of `monlog/logger` was tagged about an hour ago https://github.com/Seldaek/monolog/releases/tag/2.7.0

See this entry
> Added $datetime parameter to Logger::addRecord as low level API to allow logging into the past or future (Seldaek/monolog#1682)

Which goes along with this commit
Seldaek/monolog@0ddba73#diff-1e7fd545cec457de96f5ed6bd7249ba091cd9e699b4057db15ff1e2e0364025bR297

This changes the method signature of `addRecord` like so
```diff
-    public function addRecord(int|Level $level, string $message, array $context = []): bool
+    public function addRecord(int|Level $level, string $message, array $context = [], DateTimeImmutable $datetime = null): bool 
```

See the magento core logger is defined like this
https://github.com/magento/magento2/blob/a8543fed035d85c667195e95f69476d51f98381b/dev/tests/integration/framework/Magento/TestFramework/ErrorLog/Logger.php#L69-L73

The method signature needs to be updated to match `monolog/logger` definition
@m2-assistant
Copy link

m2-assistant bot commented Jun 9, 2022

Hi @convenient. Thank you for your contribution
Here are some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento give me test instance - deploy test instance based on PR changes
  • @magento give me 2.4-develop instance - deploy vanilla Magento instance

❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names. Allowed build names are:

  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE,
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. Semantic Version Checker

You can find more information about the builds here

ℹ️ Run only required test builds during development. Run all test builds before sending your pull request for review.

For more details, review the Magento Contributor Guide documentation.

⚠️ According to the Magento Contribution requirements, all Pull Requests must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.

🕙 You can find the schedule on the Magento Community Calendar page.

📞 The triage of Pull Requests happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

✏️ Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

@m2-github-services m2-github-services added Partner: Ampersand partners-contribution Pull Request is created by Magento Partner labels Jun 9, 2022
@convenient
Copy link
Contributor Author

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@convenient convenient changed the title Fix Test Logger monolog compatability Fix Test Logger monolog compatibility Jun 9, 2022
@convenient
Copy link
Contributor Author

convenient commented Jun 9, 2022

@magento run all tests

1 similar comment
@convenient
Copy link
Contributor Author

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@convenient
Copy link
Contributor Author

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@convenient
Copy link
Contributor Author

PHPStan detected violation(s):
 ------ ------------------------------------------------------------------- 
  Line   integration/framework/Magento/TestFramework/ErrorLog/Logger.php    
 ------ ------------------------------------------------------------------- 
  82     Method Monolog\Logger::addRecord() invoked with 4 parameters, 2-3  
         required.                                                          
 ------ ------------------------------------------------------------------- 

I dont know how your static tests are bootstrapped but evidently not pulling in the new version of monolog.

When i do a composer create project they're definitely pulling it in

@sdzhepa sdzhepa added the Priority: P1 Once P0 defects have been fixed, a defect having this priority is the next candidate for fixing. label Jun 9, 2022
@m2-community-project m2-community-project bot removed this from Pending Review in Pull Requests Dashboard Jun 9, 2022
@convenient
Copy link
Contributor Author

@magento run Unit Tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@convenient convenient mentioned this pull request Jun 10, 2022
5 tasks
@zrpnr
Copy link

zrpnr commented Jun 10, 2022

Hi @convenient you'll probably want to add use Monolog\DateTimeImmutable; after line 11 in this Logger file.

If you look at Monolog they're using their own class for DateTimeImmutable

We ran across this same problem in the Drupal monolog module: https://www.drupal.org/project/monolog/issues/3284825

@convenient
Copy link
Contributor Author

Huh thanks @zrpnr

I didn't spot that was just aiming for green tests 😅

@convenient
Copy link
Contributor Author

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@convenient
Copy link
Contributor Author

@magento run Functional Tests EE, Functional Tests CE, Functional Tests B2B

@convenient
Copy link
Contributor Author

Before I spend any time fighting test cases can I please have some feedback on the official monolog stance on what Magento is doing here, see Seldaek/monolog#1684 (comment)

Your PR works @convenient I guess but really I do wonder why Logger was extended here instead of adding a custom Handler to collect less-than-error records. That's what handlers are for, overriding addRecord for this is madness IMO. You could even do it with native handlers from monolog combining a FilterHandler and a TestHandler (or just the latter..) to gather records and access them again in the test.

Seems to me using a handler is the right thing to do here

@engcom-Alfa engcom-Alfa linked an issue Jun 21, 2022 that may be closed by this pull request
5 tasks
michielgerritsen added a commit to michielgerritsen/magento2-extension-integration-test that referenced this pull request Jun 27, 2022
michielgerritsen added a commit to michielgerritsen/magento2-extension-integration-test that referenced this pull request Jun 27, 2022
michielgerritsen added a commit to michielgerritsen/magento2-extension-integration-test that referenced this pull request Jun 27, 2022
michielgerritsen added a commit to michielgerritsen/magento2-extension-integration-test that referenced this pull request Jun 27, 2022
michielgerritsen added a commit to michielgerritsen/magento2-extension-integration-test that referenced this pull request Jun 27, 2022
michielgerritsen added a commit to michielgerritsen/magento2-extension-integration-test that referenced this pull request Jun 27, 2022
michielgerritsen added a commit to michielgerritsen/magento2-extension-integration-test that referenced this pull request Jun 27, 2022
michielgerritsen added a commit to michielgerritsen/magento2-extension-integration-test that referenced this pull request Jun 27, 2022
@ihor-sviziev
Copy link
Contributor

Looks like in Magento 2.4.5 we have a fix for this issue with the same changes 360fd1b
I believe soon the last release will be merged to 2.4-develop, so we'll see the conflict there.

@convenient
Copy link
Contributor Author

Thanks for the fyi @ihor-sviziev . I did spot this in the pre release a week or so back.

In these scenarios I wonder why my commits aren't cherry picked or merged rather than doing new ones. attribution isn't a massive deal but I've clearly done the leg work here

From identifying the issue (an hour or so after it appeared), to writing a bug report, speaking with the upstream developers, and trying to have a communication with Magento about a better way of fixing it than what was in this PR.

@ihor-sviziev
Copy link
Contributor

@convenient true!
/CC @sidolov @sdzhepa @mahesh-singh-rajawat

@sdzhepa
Copy link
Contributor

sdzhepa commented Aug 12, 2022

Hello @convenient and @ihor-sviziev

Thank you for pointing out this situation!

Unfortunately, I do not have a clear and transparent answer to "Why did it happen?"
But I agree it is absolutely not acceptable.

We are working with teams and @mahesh-singh-rajawat to prevent this kind of situation in future

Sorry for the incident and any inconveniences

cc: @sidolov

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Partner: Ampersand partners-contribution Pull Request is created by Magento Partner Priority: P1 Once P0 defects have been fixed, a defect having this priority is the next candidate for fixing.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Monolog issue
6 participants