Teach python logging formatter to use %f in the format string.
authorScott Gasch <[email protected]>
Thu, 29 Apr 2021 17:34:32 +0000 (10:34 -0700)
committerScott Gasch <[email protected]>
Thu, 29 Apr 2021 17:34:32 +0000 (10:34 -0700)
logging_utils.py

index cf784caf3d6a1c8d557f459634763d21f0d311b4..03a23d9ad063a352d4f1728e585fc6a83421958b 100644 (file)
@@ -3,9 +3,11 @@
 """Utilities related to logging."""
 
 import contextlib
+import datetime
 import logging
 from logging.handlers import RotatingFileHandler, SysLogHandler
 import os
+import pytz
 import sys
 
 import argparse_utils
@@ -40,7 +42,7 @@ parser.add_argument(
 parser.add_argument(
     '--logging_date_format',
     type=str,
-    default='%Y/%m/%dT%H:%M:%S%z',
+    default='%Y/%m/%dT%H:%M:%S.%f%z',
     metavar='DATEFMT',
     help='The format of any dates in --logging_format.'
 )
@@ -96,6 +98,19 @@ class OnlyInfoFilter(logging.Filter):
         return record.levelno == logging.INFO
 
 
+class MillisecondAwareFormatter(logging.Formatter):
+    converter = datetime.datetime.fromtimestamp
+
+    def formatTime(self, record, datefmt=None):
+        ct = self.converter(record.created, pytz.timezone("US/Pacific"))
+        if datefmt:
+            s = ct.strftime(datefmt)
+        else:
+            t = ct.strftime("%Y-%m-%d %H:%M:%S")
+            s = "%s,%03d" % (t, record.msecs)
+        return s
+
+
 def initialize_logging(logger=None) -> logging.Logger:
     assert config.has_been_parsed()
     if logger is None:
@@ -129,7 +144,7 @@ def initialize_logging(logger=None) -> logging.Logger:
 #            for k, v in encoded_priorities.items():
 #                handler.encodePriority(k, v)
             handler.setFormatter(
-                logging.Formatter(
+                MillisecondAwareFormatter(
                     fmt=fmt,
                     datefmt=config.config['logging_date_format'],
                 )
@@ -145,7 +160,7 @@ def initialize_logging(logger=None) -> logging.Logger:
         )
         handler.setLevel(numeric_level)
         handler.setFormatter(
-            logging.Formatter(
+            MillisecondAwareFormatter(
                 fmt=fmt,
                 datefmt=config.config['logging_date_format'],
             )
@@ -156,7 +171,7 @@ def initialize_logging(logger=None) -> logging.Logger:
         handler = logging.StreamHandler(sys.stderr)
         handler.setLevel(numeric_level)
         handler.setFormatter(
-            logging.Formatter(
+            MillisecondAwareFormatter(
                 fmt=fmt,
                 datefmt=config.config['logging_date_format'],
             )