Skip to content

Commit 06a3554

Browse files
authored
bpo-40300: Allow empty logging.Formatter.default_msec_format. (GH-19551)
1 parent 1ae035b commit 06a3554

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

Doc/library/logging.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,9 @@ The useful mapping keys in a :class:`LogRecord` are given in the section on
608608
attributes are ``default_time_format`` (for the strptime format string)
609609
and ``default_msec_format`` (for appending the millisecond value).
610610

611+
.. versionchanged:: 3.9
612+
The ``default_msec_format`` can be ``None``.
613+
611614
.. method:: formatException(exc_info)
612615

613616
Formats the specified exception information (a standard exception tuple as

Lib/logging/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -597,8 +597,9 @@ def formatTime(self, record, datefmt=None):
597597
if datefmt:
598598
s = time.strftime(datefmt, ct)
599599
else:
600-
t = time.strftime(self.default_time_format, ct)
601-
s = self.default_msec_format % (t, record.msecs)
600+
s = time.strftime(self.default_time_format, ct)
601+
if self.default_msec_format:
602+
s = self.default_msec_format % (s, record.msecs)
602603
return s
603604

604605
def formatException(self, ei):

Lib/test/test_logging.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3941,6 +3941,19 @@ def test_time(self):
39413941
f.format(r)
39423942
self.assertEqual(r.asctime, '1993-04-21 08:03:00,123')
39433943

3944+
def test_default_msec_format_none(self):
3945+
class NoMsecFormatter(logging.Formatter):
3946+
default_msec_format = None
3947+
default_time_format = '%d/%m/%Y %H:%M:%S'
3948+
3949+
r = self.get_record()
3950+
dt = datetime.datetime(1993, 4, 21, 8, 3, 0, 123, utc)
3951+
r.created = time.mktime(dt.astimezone(None).timetuple())
3952+
f = NoMsecFormatter()
3953+
f.converter = time.gmtime
3954+
self.assertEqual(f.formatTime(r), '21/04/1993 08:03:00')
3955+
3956+
39443957
class TestBufferingFormatter(logging.BufferingFormatter):
39453958
def formatHeader(self, records):
39463959
return '[(%d)' % len(records)

0 commit comments

Comments
 (0)