From 3713c848100f5248a685d896a5e23893a7bf66d3 Mon Sep 17 00:00:00 2001 From: Dario Borreguero Rincon Date: Wed, 16 Oct 2024 08:53:21 +0200 Subject: [PATCH] #11 trying to address review comments on PR --- src/sysdiagnose/main.py | 37 ++------------------- src/sysdiagnose/parsers/powerlogs.py | 5 +-- src/sysdiagnose/utils/base.py | 4 +-- src/sysdiagnose/utils/jsonlogger.py | 9 ------ src/sysdiagnose/utils/logger.py | 48 ++++++++++++++++++++++++++++ 5 files changed, 53 insertions(+), 50 deletions(-) delete mode 100644 src/sysdiagnose/utils/jsonlogger.py create mode 100644 src/sysdiagnose/utils/logger.py diff --git a/src/sysdiagnose/main.py b/src/sysdiagnose/main.py index 7f6478e..a82c7db 100644 --- a/src/sysdiagnose/main.py +++ b/src/sysdiagnose/main.py @@ -3,13 +3,9 @@ import sys from sysdiagnose import Sysdiagnose import os -import json -import logging import time -from sysdiagnose.utils.jsonlogger import SysdiagnoseJsonFormatter +from sysdiagnose.utils.logger import logger, get_console_handler, get_json_handler -logger = logging.getLogger('sysdiagnose') -logger.setLevel(logging.INFO) def parse_parser_error(message): sd = Sysdiagnose() @@ -28,33 +24,6 @@ def analyse_parser_error(message): sd.print_analysers_list() sys.exit(2) -def get_console_logger(level: str) -> logging.StreamHandler: - # Format - fmt_console = logging.Formatter('[%(levelname)s] [%(module)s] %(message)s') - # Console handler - ch = logging.StreamHandler() - ch.setLevel(level) - ch.setFormatter(fmt_console) - - return ch - -def get_json_logger(filename: str) -> logging.FileHandler: - # https://stackoverflow.com/questions/50144628/python-logging-into-file-as-a-dictionary-or-json - # fmt_json = logging.Formatter( - # json.dumps({ - # 'timestamp':'%(asctime)s', - # 'level': '%(levelname)s', - # 'module': '%(module)s', - # 'message': '%(message)s'})) - fmt_json = SysdiagnoseJsonFormatter( - fmt='%(asctime)s %(levelname)s %(module)s %(message)s', - rename_fields={'asctime': 'timestamp'}) - # File handler - fh = logging.FileHandler(filename) - fh.setLevel(logging.INFO) - fh.setFormatter(fmt_json) - - return fh def main(): parser = argparse.ArgumentParser( @@ -160,7 +129,7 @@ def main(): # Handle console logging log_level = args.log.upper() - logger.addHandler(get_console_logger(log_level)) + logger.addHandler(get_console_handler(log_level)) logger2file = None for case_id in case_ids: @@ -170,7 +139,7 @@ def main(): folder = sd.config.get_case_parsed_data_folder(case_id) # https://stackoverflow.com/questions/13839554/how-to-change-filehandle-with-python-logging-on-the-fly-with-different-classes-a if logger2file is None: - logger2file = get_json_logger(os.path.join(folder, filename)) + logger2file = get_json_handler(os.path.join(folder, filename)) logger.addHandler(logger2file) else: logger2file.close() diff --git a/src/sysdiagnose/parsers/powerlogs.py b/src/sysdiagnose/parsers/powerlogs.py index ee0ae2d..718122f 100644 --- a/src/sysdiagnose/parsers/powerlogs.py +++ b/src/sysdiagnose/parsers/powerlogs.py @@ -7,11 +7,8 @@ from sysdiagnose.utils import sqlite2json import glob import os -from sysdiagnose.utils.base import BaseParserInterface +from sysdiagnose.utils.base import BaseParserInterface, logger from datetime import datetime, timezone -import logging - -logger = logging.getLogger('sysdiagnose') class PowerLogsParser(BaseParserInterface): diff --git a/src/sysdiagnose/utils/base.py b/src/sysdiagnose/utils/base.py index 3ca4017..ac87f70 100644 --- a/src/sysdiagnose/utils/base.py +++ b/src/sysdiagnose/utils/base.py @@ -6,9 +6,7 @@ from datetime import datetime import re from functools import cached_property -import logging - -logger = logging.getLogger('sysdiagnose') +from sysdiagnose.utils.logger import logger class SysdiagnoseConfig: diff --git a/src/sysdiagnose/utils/jsonlogger.py b/src/sysdiagnose/utils/jsonlogger.py deleted file mode 100644 index c9e99f6..0000000 --- a/src/sysdiagnose/utils/jsonlogger.py +++ /dev/null @@ -1,9 +0,0 @@ -from pythonjsonlogger import jsonlogger -from datetime import datetime - - -class SysdiagnoseJsonFormatter(jsonlogger.JsonFormatter): - '''Custom JSON logger formatter ''' - # https://stackoverflow.com/questions/50873446/python-logger-output-dates-in-is8601-format - def formatTime(self, record, datefmt=None): - return datetime.fromtimestamp(record.created).astimezone().isoformat(timespec='microseconds') diff --git a/src/sysdiagnose/utils/logger.py b/src/sysdiagnose/utils/logger.py new file mode 100644 index 0000000..cc6ed8c --- /dev/null +++ b/src/sysdiagnose/utils/logger.py @@ -0,0 +1,48 @@ +import logging +from pythonjsonlogger import jsonlogger +from datetime import datetime + +logger = logging.getLogger('sysdiagnose') +# By default, we want to have the possibility to log almost everything. +logger.setLevel(logging.INFO) + +class SysdiagnoseJsonFormatter(jsonlogger.JsonFormatter): + '''Custom JSON logger formatter ''' + # https://stackoverflow.com/questions/50873446/python-logger-output-dates-in-is8601-format + def formatTime(self, record, datefmt=None): + return datetime.fromtimestamp(record.created).astimezone().isoformat(timespec='microseconds') + + +def get_console_handler(level: str) -> logging.StreamHandler: + ''' + Creates a logging stream handler. + + Args: + level: Logging level. https://docs.python.org/3/library/logging.html#logging-levels + ''' + # Format + fmt_console = logging.Formatter('[%(levelname)s] [%(module)s] %(message)s') + # Console handler + ch = logging.StreamHandler() + ch.setLevel(level) + ch.setFormatter(fmt_console) + + return ch + +def get_json_handler(filename: str, level: int = logging.INFO) -> logging.FileHandler: + ''' + Creates a logging JSON format file handler. + + Args: + filename: Filename where to log. + level: Logging level. By default to INFO. https://docs.python.org/3/library/logging.html#logging-levels + ''' + fmt_json = SysdiagnoseJsonFormatter( + fmt='%(asctime)s %(levelname)s %(module)s %(message)s', + rename_fields={'asctime': 'timestamp'}) + # File handler + fh = logging.FileHandler(filename) + fh.setLevel(level) + fh.setFormatter(fmt_json) + + return fh