Background
This link https://mcmap.net/q/107886/-python-logging-use-milliseconds-in-time-format provides a solution to format logging timestamp with milliseconds, which requires overriding the method formatTime
in logging.Formatter
class.
With some modifications of the solutions in that link, I could produce a logging timestamp that is compatiable with ISO8601 format
import logging
from datetime import datetime
import pytz
class MyFormatter(logging.Formatter):
# override the converter in logging.Formatter
converter = datetime.fromtimestamp
# override formatTime in logging.Formatter
def formatTime(self, record, datefmt=None, timezone="UTC"):
return self.converter(record.created, tz=pytz.timezone(timezone)).isoformat()
logger = logging.getLogger(__name__)
logger.setLevel(level="DEBUG")
console = logging.StreamHandler()
logger.addHandler(console)
formatter = MyFormatter(fmt="%(asctime)s - %(levelname)s - %(message)s")
console.setFormatter(formatter)
logger.debug("Testing")
The console output is
2020-08-20T08:37:08.934591+00:00 - DEBUG - Testing
Question
But my python project has multiple modules, and most of them require logging.
Do I need to repeatedly setup the
logger
for each module as shown in the above snippet?Is there any solution that is as clean as simply using a logging config file and execute something like this
logging.config.dictConfig(logging_conf)
?
logging.Formatter
'sformatTime
at the class level. – Dissonant