From 04d1e16ba0ec31481c4a0848b652956da19d0d96 Mon Sep 17 00:00:00 2001 From: KazunoriSumiya Date: Wed, 24 Dec 2025 17:24:32 +0900 Subject: [PATCH] feature_log_onoff (#141) Copy modify logger.py from mct and modify LOGGER_NAME. Removed the process of calling the set_log_folder function of mctq from mct. --- mct_quantizers/logger.py | 116 ++++++++++++++++++++++++--------------- 1 file changed, 73 insertions(+), 43 deletions(-) diff --git a/mct_quantizers/logger.py b/mct_quantizers/logger.py index d369943..d7be82d 100644 --- a/mct_quantizers/logger.py +++ b/mct_quantizers/logger.py @@ -1,4 +1,4 @@ -# Copyright 2023 Sony Semiconductor Solutions, Inc. All rights reserved. +# Copyright 2025 Sony Semiconductor Solutions, Inc. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -18,60 +18,88 @@ import os from datetime import datetime from pathlib import Path +from typing import Optional LOGGER_NAME = 'MCT Quantizers' class Logger: # Logger has levels of verbosity. - log_level_translate = { - 'debug': logging.DEBUG, - 'info': logging.INFO, - 'warning': logging.WARNING, - 'error': logging.ERROR, - 'critical': logging.CRITICAL - } LOG_PATH = None + DEBUG = logging.DEBUG + INFO = logging.INFO + WARNING = logging.WARNING + ERROR = logging.ERROR + CRITICAL = logging.CRITICAL @staticmethod - def __check_path_create_dir(log_path: str): + def __check_path_create_dir(log_path: str) -> None: """ Create a path if not exist. Otherwise, do nothing. - Args: - log_path: Path to create or verify that exists. + Args: + log_path (str): Path to create or verify that exists. """ if not os.path.exists(log_path): Path(log_path).mkdir(parents=True, exist_ok=True) @staticmethod - def set_logger_level(log_level=logging.INFO): + def set_logger_level(log_level: int = logging.INFO) -> None: """ Set log level to determine the logger verbosity. - Args: - log_level: Level of verbosity to set for the logger. + Args: + log_level (int): Level of verbosity to set for the logger. """ logger = Logger.get_logger() logger.setLevel(log_level) @staticmethod - def get_logger(): + def set_handler_level(log_level: int = logging.INFO) -> None: + """ + Set log level for all handlers attached to the logger. + + Args: + log_level (int): Level of verbosity to set for the handlers. + """ + + logger = Logger.get_logger() + for handler in logger.handlers: + handler.setLevel(log_level) + + @staticmethod + def get_logger() -> logging.Logger: """ Returns: An instance of the logger. """ return logging.getLogger(LOGGER_NAME) @staticmethod - def set_log_file(log_folder: str = None): + def set_stream_handler() -> None: + """ + Add a StreamHandler to output logs to the console (stdout). + """ + logger = Logger.get_logger() + + # Check if StreamHandler already exists + for handler in logger.handlers: + if isinstance(handler, logging.StreamHandler): + return + + # Add StreamHandler + sh = logging.StreamHandler() + logger.addHandler(sh) + + @staticmethod + def set_log_file(log_folder: Optional[str] = None) -> None: """ Setting the logger log file path. The method gets the folder for the log file. In that folder, it creates a log file according to the timestamp. - Args: - log_folder: Folder path to hold the log file. + Args: + log_folder (Optional[str]): Folder path to hold the log file. """ logger = Logger.get_logger() @@ -87,16 +115,14 @@ def set_log_file(log_folder: str = None): Logger.__check_path_create_dir(Logger.LOG_PATH) fh = logging.FileHandler(log_name) - fh.setLevel(logging.DEBUG) logger.addHandler(fh) print(f'log file is in {log_name}') @staticmethod - def shutdown(): + def shutdown() -> None: """ An orderly command to shutdown by flushing and closing all logging handlers. - """ Logger.LOG_PATH = None logging.shutdown() @@ -106,81 +132,85 @@ def shutdown(): ######################################## @staticmethod - def critical(msg: str): + def critical(msg: str) -> None: """ Log a message at 'critical' severity and raise an exception. - Args: - msg: Message to log. + Args: + msg (str): Message to log. """ Logger.get_logger().critical(msg) raise Exception(msg) @staticmethod - def exception(msg: str): + def exception(msg: str) -> None: """ Log a message at 'exception' severity and raise an exception. - Args: - msg: Message to log. + Args: + msg (str): Message to log. """ Logger.get_logger().exception(msg) raise Exception(msg) @staticmethod - def debug(msg: str): + def debug(msg: str) -> None: """ Log a message at 'debug' severity. Args: - msg: Message to log. - + msg (str): Message to log. """ Logger.get_logger().debug(msg) @staticmethod - def info(msg: str): + def info(msg: str) -> None: """ Log a message at 'info' severity. Args: - msg: Message to log. - + msg (str): Message to log. """ Logger.get_logger().info(msg) @staticmethod - def warning(msg: str): + def warning(msg: str) -> None: """ Log a message at 'warning' severity. Args: - msg: Message to log. - + msg (str): Message to log. """ Logger.get_logger().warning(msg) @staticmethod - def error(msg: str): + def error(msg: str) -> None: """ Log a message at 'error' severity and raise an exception. Args: - msg: Message to log. - + msg (str): Message to log. """ Logger.get_logger().error(msg) - raise Exception(msg) -def set_log_folder(folder: str, level: int = logging.INFO): +def set_log_folder(folder: str, level: int = logging.INFO) -> None: """ Set a directory path for saving a log file. Args: - folder: Folder path to save the log file. - level: Level of verbosity to set to the logger. + folder (str): Folder path to save the log file. + level (int): Level of verbosity to set to the logger and handlers. + + Note: + This is a convenience function that calls multiple Logger methods + to set up logging. + Don't use Python's original logger. """ + + Logger.set_stream_handler() Logger.set_log_file(folder) Logger.set_logger_level(level) + Logger.set_handler_level(level) +