Skip to content

gh-102402: Fix floating point math issue by using time.time_ns() in logging.LogRecord#102412

Merged
vsajip merged 24 commits intopython:mainfrom
dougthor42:fix-gh-102402-logging-msecs
Apr 16, 2024
Merged

gh-102402: Fix floating point math issue by using time.time_ns() in logging.LogRecord#102412
vsajip merged 24 commits intopython:mainfrom
dougthor42:fix-gh-102402-logging-msecs

Conversation

@dougthor42
Copy link
Contributor

@dougthor42 dougthor42 commented Mar 4, 2023

gh-102402: Fix floating point math issue by using time.time_ns() in logging.LogRecord.

  • Adjust logging.LogRecord to use time.time_ns() instead of time.time().
  • Change method of calculating LogRecord.msecs to be robust against floating point / binary representation arithmetic errors
  • Added tests for the changes.

Notes / Open Questions:

  1. LogRecord.msecs is a float so I kept it that way. Why is it a float even though it always has .0?
  2. In order to minimize impact, I kept LogRecord.created as a float in seconds.
  3. I didn't see any type annotations in logging.__init__ so I didn't add any for the new code. Would you like me to add some annotations?
  4. I opted to use string manipulation to grab the msecs. I have an integer math-based approach, but it's, IMO, more verbose and harder to read/understand. LMK if you want me to switch back to that (there are still some minor fixes to apply to it).
    • I was able to come up with a math approach that is easy enough to understand.
  5. I wasn't sure of the best place to put the tests, so I just built off the other msecs-related bug.

Fixes #102402.

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

Labels

stdlib Standard Library Python modules in the Lib/ directory

Projects

Development

Successfully merging this pull request may close these issues.

Logging's msecs doesn't handle "100ms" well.

6 participants