Skip to content

bpo-40300: Allow empty logging.Formatter.default_msec_format. (GH-19551) #19551

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

Merged
merged 1 commit into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Doc/library/logging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,9 @@ The useful mapping keys in a :class:`LogRecord` are given in the section on
attributes are ``default_time_format`` (for the strptime format string)
and ``default_msec_format`` (for appending the millisecond value).

.. versionchanged:: 3.9
The ``default_msec_format`` can be ``None``.

.. method:: formatException(exc_info)

Formats the specified exception information (a standard exception tuple as
Expand Down
5 changes: 3 additions & 2 deletions Lib/logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,8 +597,9 @@ def formatTime(self, record, datefmt=None):
if datefmt:
s = time.strftime(datefmt, ct)
else:
t = time.strftime(self.default_time_format, ct)
s = self.default_msec_format % (t, record.msecs)
s = time.strftime(self.default_time_format, ct)
if self.default_msec_format:
s = self.default_msec_format % (s, record.msecs)
return s

def formatException(self, ei):
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -3941,6 +3941,19 @@ def test_time(self):
f.format(r)
self.assertEqual(r.asctime, '1993-04-21 08:03:00,123')

def test_default_msec_format_none(self):
class NoMsecFormatter(logging.Formatter):
default_msec_format = None
default_time_format = '%d/%m/%Y %H:%M:%S'

r = self.get_record()
dt = datetime.datetime(1993, 4, 21, 8, 3, 0, 123, utc)
r.created = time.mktime(dt.astimezone(None).timetuple())
f = NoMsecFormatter()
f.converter = time.gmtime
self.assertEqual(f.formatTime(r), '21/04/1993 08:03:00')


class TestBufferingFormatter(logging.BufferingFormatter):
def formatHeader(self, records):
return '[(%d)' % len(records)
Expand Down