diff --git a/src/rhsm/logutil.py b/src/rhsm/logutil.py index 10e7220167..dbda660fbc 100644 --- a/src/rhsm/logutil.py +++ b/src/rhsm/logutil.py @@ -10,20 +10,13 @@ from typing import Optional, Tuple, Union, List +import datetime import logging import logging.handlers import logging.config import os import sys import rhsm.config -import datetime - - -class RhsmISO8601Formatter(logging.Formatter): - """Ensure date & time to be in ISO8601 format and containing info about milliseconds and time zone""" - - def formatTime(self, record, datefmt=None): - return datetime.datetime.fromtimestamp(record.created).astimezone().isoformat(timespec="milliseconds") LOGFILE_DIR = "/var/log/rhsm/" @@ -39,8 +32,6 @@ def formatTime(self, record, datefmt=None): "%(threadName)s @%(filename)s:%(lineno)d - %(message)s" ) -LOG_FORMATTER = RhsmISO8601Formatter(LOG_FORMAT) - _rhsm_log_handler: Optional["RHSMLogHandler"] = None _subman_debug_handler: Optional["SubmanDebugHandler"] = None log: Optional[logging.Logger] = None @@ -155,6 +146,15 @@ def __init__(self, name) -> None: self.setLevel(self.level) self.addFilter(PyWarningsLoggingFilter(name="py.warnings")) +class RhsmISO8601Formatter(logging.Formatter): + """Ensure date & time to be in ISO8601 format and containing info about milliseconds and time zone""" + + def __init__(self): + super().__init__(fmt=LOG_FORMAT) + + def formatTime(self, record, datefmt=None): + return datetime.datetime.fromtimestamp(record.created).astimezone().isoformat(timespec="milliseconds") + def _get_default_rhsm_log_handler() -> ( Tuple[Union[logging.handlers.RotatingFileHandler, logging.StreamHandler], Optional[str]] @@ -163,7 +163,7 @@ def _get_default_rhsm_log_handler() -> ( error: Optional[Exception] = None if not _rhsm_log_handler: _rhsm_log_handler, error = RHSMLogHandler(LOGFILE_PATH, USER_LOGFILE_PATH) - _rhsm_log_handler.setFormatter(LOG_FORMATTER) + _rhsm_log_handler.setFormatter(RhsmISO8601Formatter()) return _rhsm_log_handler, error @@ -171,7 +171,7 @@ def _get_default_subman_debug_handler() -> Union[None, "SubmanDebugHandler"]: global _subman_debug_handler if not _subman_debug_handler: _subman_debug_handler = SubmanDebugHandler() - _subman_debug_handler.setFormatter(LOG_FORMATTER) + _subman_debug_handler.setFormatter(RhsmISO8601Formatter()) return _subman_debug_handler