Python Logging: ISO8601 timestamp with milliseconds and timezone using config file

Viewed 1104

Background

This link https://stackoverflow.com/a/6290946/6484585 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.

  1. Do I need to repeatedly setup the logger for each module as shown in the above snippet?

  2. 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) ?

0 Answers
Related