"""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
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.'
)
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:
# for k, v in encoded_priorities.items():
# handler.encodePriority(k, v)
handler.setFormatter(
- logging.Formatter(
+ MillisecondAwareFormatter(
fmt=fmt,
datefmt=config.config['logging_date_format'],
)
)
handler.setLevel(numeric_level)
handler.setFormatter(
- logging.Formatter(
+ MillisecondAwareFormatter(
fmt=fmt,
datefmt=config.config['logging_date_format'],
)
handler = logging.StreamHandler(sys.stderr)
handler.setLevel(numeric_level)
handler.setFormatter(
- logging.Formatter(
+ MillisecondAwareFormatter(
fmt=fmt,
datefmt=config.config['logging_date_format'],
)