3

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

2
  • Also interested in this. Did you find an answer? As of now, my solution was to use UTC time for the logger and adapt the log format to include "msecs" in addition to "asctime". It works for my usecase, but I'd like to know more on this. Commented Mar 23, 2021 at 11:02
  • See stackoverflow.com/a/67821131/594211. You can override logging.Formatter's formatTime at the class level. Commented Jun 3, 2021 at 12:15

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.