]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-40300: Allow empty logging.Formatter.default_msec_format. (GH-19551)
authorMariusz Felisiak <felisiak.mariusz@gmail.com>
Fri, 17 Apr 2020 16:02:47 +0000 (18:02 +0200)
committerGitHub <noreply@github.com>
Fri, 17 Apr 2020 16:02:47 +0000 (17:02 +0100)
Doc/library/logging.rst
Lib/logging/__init__.py
Lib/test/test_logging.py

index e943011c8afd8437178054b19ec9815ccf6cfc56..7267f812cc1925bdea4a9c14f5e7e514c3632194 100644 (file)
@@ -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
index 84a177559908a2c31aba8488dd1db73f5b4f78d1..403dc81b13ef4e235b8300a6d867b8e941ef6325 100644 (file)
@@ -597,8 +597,9 @@ class Formatter(object):
         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):
index 2ad3c5c20858391498d5001ae2bf822c7f589460..99e74ebff60875560c3ad6d09a88f347dfe61859 100644 (file)
@@ -3941,6 +3941,19 @@ class FormatterTest(unittest.TestCase):
         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)