Skip to content

Commit 6fbd889

Browse files
authored
gh-89047: Fix msecs computation so you never end up with 1000 msecs. (GH-96340)
1 parent 013e659 commit 6fbd889

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

Lib/logging/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ def __init__(self, name, level, pathname, lineno,
340340
self.lineno = lineno
341341
self.funcName = func
342342
self.created = ct
343-
self.msecs = (ct - int(ct)) * 1000
343+
self.msecs = int((ct - int(ct)) * 1000) + 0.0 # see gh-89047
344344
self.relativeCreated = (self.created - _startTime) * 1000
345345
if logThreads:
346346
self.thread = threading.get_ident()

Lib/test/test_logging.py

+8
Original file line numberDiff line numberDiff line change
@@ -4261,6 +4261,14 @@ class NoMsecFormatter(logging.Formatter):
42614261
f.converter = time.gmtime
42624262
self.assertEqual(f.formatTime(r), '21/04/1993 08:03:00')
42634263

4264+
def test_issue_89047(self):
4265+
f = logging.Formatter(fmt='{asctime}.{msecs:03.0f} {message}', style='{', datefmt="%Y-%m-%d %H:%M:%S")
4266+
for i in range(2500):
4267+
time.sleep(0.0004)
4268+
r = logging.makeLogRecord({'msg': 'Message %d' % (i + 1)})
4269+
s = f.format(r)
4270+
self.assertNotIn('.1000', s)
4271+
42644272

42654273
class TestBufferingFormatter(logging.BufferingFormatter):
42664274
def formatHeader(self, records):

0 commit comments

Comments
 (0)