mirror of
https://github.com/Seldaek/monolog.git
synced 2026-04-26 08:05:53 +03:00
[GH-ISSUE #438] Fix the "datetime" field/add "timestamp" field #153
Labels
No labels
Bug
Documentation
Feature
Needs Work
Support
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/monolog#153
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @giannello on GitHub (Oct 21, 2014).
Original GitHub issue: https://github.com/Seldaek/monolog/issues/438
As per current implementation, monolog is not able to provide high-resolution timestamps for logs, because of the use of the DateTime object. Also - despite what the v1.3.0 changelog states (Support for millisecond in timestamps) - the feature is actually not more than a mock and the test case itself is not optimal, as it tests an edge case.
Right now, the only way to make sure logs coming out from monolog are accurate is to rely on the output sequence of the messages. While this case is acceptable when using file/stdout output, it's not valid anymore when outputting to an AMQP broker for later consumption.
In this case, as AMQP does not guarantee an ordered delivery, any indexer (InfluxDB, in our current implementation) will need a higher-resolution timestamp to make sure the log messages can be indexed/viewed in the correct order.
As log messages are not more than description of events in time, the best solution would be to just store the unix timestamp with microseconds, and let the presentation layer convert them to the appropriate date/time format the user needs.
@Seldaek commented on GitHub (Oct 21, 2014):
That might have been a better approach but it'd be a BC break at this point. Can you explain in more details why the current DateTime solution does not work though? As far as I know if you do format it correctly on output you will get 1/10th of millisecond precision (i.e. 4 digits after the comma, which is what microtime() gives us).
@giannello commented on GitHub (Oct 21, 2014):
well, with the current approach, serializing the DateTime object to JSON will not guarantee the right precision - but this is being addressed in #402
The problem with the formatting is actually related to the way php handles rounding floats when they are printed:
any higher precision will result in zeroes added after the microsecond value.
A possible, non-breaking solution would be to add a config parameter that enables adding a "timestamp" field, and that field - to avoid arbitrary rounding - should be a string and not a float
Not the best way, as a proper numeric type should be used, but consistent.
I'm working on a fix in my fork, might be able to submit it today.
@giannello commented on GitHub (Oct 21, 2014):
Not the perfect solution - the timestamp generated will be slightly (milliseconds) different from the real timestamp of the message ($date->format('U.u') returns a value with 0 microseconds) - but still better than nothing, and it's fully backwards-compatible
@Seldaek commented on GitHub (May 26, 2016):
As far as I can tell this was fixed by https://github.com/Seldaek/monolog/pull/793 - in 2.0 if you enable microsecond precision then json-serialized datetime objects will have full precision.