From: Scott Gasch Date: Thu, 29 Apr 2021 17:34:32 +0000 (-0700) Subject: Teach python logging formatter to use %f in the format string. X-Git-Url: https://wannabe.guru.org/gitweb/?a=commitdiff_plain;h=5fd30ef12c100cbb936aa0fdb515b67cff4064db;p=python_utils.git Teach python logging formatter to use %f in the format string. --- diff --git a/logging_utils.py b/logging_utils.py index cf784ca..03a23d9 100644 --- a/logging_utils.py +++ b/logging_utils.py @@ -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'], )