8000 Logging's msecs doesn't handle "100ms" well. · Issue #102402 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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

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
dougthor42 opened this issue Mar 3, 2023 · 2 comments · Fixed by #102412
Closed

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

dougthor42 opened this issue Mar 3, 2023 · 2 comments · Fixed by #102412
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@dougthor42
Copy link
Contributor
dougthor42 commented Mar 3, 2023

Bug report

LogRecord.msecs returns an incorrect value when timestamp (self.ct) value has exactly 100ms.

One liner check:

assert int((1677793338.100_000_0 - int(1677793338.100_000_0)) * 1000) + 0.0 == 100.0

The issue is binary representation of "0.1" / floating point error:

>>> # Definition of LogRecord.msecs:
>>> # https://github.com/python/cpython/blob/12011dd8bafa6867f2b4a8a9e8e54cb0fbf006e4/Lib/logging/__init__.py#L343
>>> # int((ct - int(ct)) * 1000) + 0.0
>>> ct = 1677793338.100_000_0
>>> ct
1677793338.1
>>> ct - int(ct)
0.09999990463256836
>>> _ * 1000
99.99990463256836
>>> int(_)
99
>>> _ + 0.0
99.0

Your environment

  • CPython versions tested on:
    • 3.10.9
    • 3.11.2
  • Operating system and architecture:
    • Custom company OS based on Debian Testing
    • 64-bit

Discussion

I think switching to time.time_ns when setting the creation time might be one solution.

Linked PRs

@dougthor42 dougthor42 added the type-bug An unexpected behavior, bug, or error label Mar 3, 2023
@AlexWaygood AlexWaygood added the stdlib Python modules in the Lib dir label Mar 3, 2023
@vacosti
Copy link
vacosti commented Mar 3, 2023

I agree with @dougthor42 about the proposed solution, since time library itself suggests to "use time_ns() to avoid the precision loss caused by the float type".

@dougthor42
Copy link
Contributor Author

I hope to find some time this weekend to implement a fix.

vsajip pushed a commit that referenced this issue Apr 16, 2024
diegorusso pushed a commit to diegorusso/cpython that referenced this issue Apr 17, 2024
vstinner added a commit to vstinner/cpython that referenced this issue Apr 17, 2024
…n() leak

Fix a reference leak in test_relativeCreated_has_higher_precision()
of test_logging: don't reimport the logging the logging module.
vstinner added a commit that referenced this issue Apr 18, 2024
…ak (#117985)

Fix a reference leak in test_relativeCreated_has_higher_precision()
of test_logging: don't reimport the logging the logging module.
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Apr 18, 2024
SonicField pushed a commit to SonicField/cpython that referenced this issue May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
Development

Successfully merging a pull request may close this issue.

3 participants
0