Skip to content

Possibility to overwrite the datetime of a record #1682

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

Closed
wants to merge 0 commits into from

Conversation

Henning256
Copy link

Added an optional parameter to the loggers addRecord method to overwrite the datetime of a record.
This is useful to decouple the time a record is issued and when it is persisted.
For example when caching the records to log them asynchronous.

@Seldaek
Copy link
Owner

Seldaek commented Jun 8, 2022

This sounds reasonable albeit kinda niche.. But adding it only to addRecord sounds ok to me as that's very low level.

Would you mind rebasing on the 2.x branch tho? I think this probably should go there as well as the 3.x adoption is very low still.

@Seldaek Seldaek added this to the 2.x milestone Jun 8, 2022
@Seldaek Seldaek added the Feature label Jun 8, 2022
@Seldaek Seldaek closed this Jun 9, 2022
@Seldaek
Copy link
Owner

Seldaek commented Jun 9, 2022

Ah well, I messed up and pushed main into your main instead of a local branch I had to do the rebase, so that closed the PR..

Anyway the code is merged in 2.x branch as 24e414c - thanks :)

convenient added a commit to convenient/mtwo that referenced this pull request Jun 9, 2022
# 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
@WoZ
Copy link
Contributor

WoZ commented Jun 14, 2022

@Henning256 @Seldaek I've found this issue in the 2.7/3.1 changelog. @Henning256 I understand your approach and solved the same issue a few weeks ago, but I decided to store 2 times: when the log record was generated and when the log record was dispatched. It's just a tip, not to forget to log both of them, it will help to debug queues of log entries and delays in the dispatching. Cheers.

@Henning256
Copy link
Author

@Henning256 @Seldaek I've found this issue in the 2.7/3.1 changelog. @Henning256 I understand your approach and solved the same issue a few weeks ago, but I decided to store 2 times: when the log record was generated and when the log record was dispatched. It's just a tip, not to forget to log both of them, it will help to debug queues of log entries and delays in the dispatching. Cheers.

Hi and thank you.
This sounds like something that belongs in your handler.
i.e. I wrote a Handler that logs to the database which logs the timestamp from the record as "issued_at" and the actual time as "created_at".

As I mentioned in my PR I implemented this feature for an async logging solution which works like this:
I have a handler that writes the log record along with a destination channel into the cache.
Then I implemented a Job that reads the cached records and logs them to their destination channels.
And this is exactly the point where the new feature comes in because now I can use the original timestamp from the record to log the record with the addRecord function to its destination channel and not loose the information when the record was issued.

Hope that helped someone.

Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants