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

0

3

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.

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

Terrific answered 20/8, 2020 at 8:48 Comment(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.Famish
See https://mcmap.net/q/107886/-python-logging-use-milliseconds-in-time-format. You can override logging.Formatter's formatTime at the class level.Dissonant

© 2022 - 2024 — McMap. All rights reserved.