From f7c7b5e4ad710f02437637e005ac2c2c66eb09ad Mon Sep 17 00:00:00 2001 From: "jj-author@users.noreply.github.com" Date: Wed, 23 Oct 2024 16:51:20 +0200 Subject: [PATCH] first temporary PoC fix of logging setup --- ontologytimemachine/utils/config.py | 80 ++++++++++++++++++++++++++--- 1 file changed, 73 insertions(+), 7 deletions(-) diff --git a/ontologytimemachine/utils/config.py b/ontologytimemachine/utils/config.py index e49ea27..4f962e3 100644 --- a/ontologytimemachine/utils/config.py +++ b/ontologytimemachine/utils/config.py @@ -5,10 +5,28 @@ from typing import Dict, Any, Type, TypeVar -logging.basicConfig( - level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s" -) -logger = logging.getLogger(__name__) +# logging.basicConfig( +# level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s" +# ) +# logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s") +# '%(asctime)s - pid:%(process)d [%(levelname)-.1s] %(module)s.%(funcName)s:%(lineno)d - %(message)s' + # Logger.setup(args.log_file, args.log_level, args.log_format) + +def _print_logger_info(context_info: str, logger: logging.Logger) -> None : + print(context_info+" logger name:"+logger.name) + print(context_info+" logger id:"+ str(id(logger))) + print(context_info+" logger effictive Level:"+str(logger.getEffectiveLevel)) + print(context_info+" logger logLevel:"+str(logger.level)) + print(context_info+" logger parent:"+str(logger.parent)) + print(context_info+" logger handlers:"+str(logger.handlers)) + print(context_info+" logger parent handlers:"+str(logger.parent.handlers)) + +loggerpp = logging.getLogger(__name__) +#_print_logger_info("config.py getLogger(__name__)",loggerpp) +logger = logging.getLogger("ontologytimemachine.utils.config") +logger = logging.getLogger("ontologytimemachine.utils.config") +#_print_logger_info("config.py getLogger(ontologytimemachine.utils.config)",loggerpp) + class EnumValuePrint( @@ -96,6 +114,28 @@ def enum_parser(enum_class: Type[E], value: str) -> E: f"Invalid value '{value}'. Available options are: {valid_options}" ) from exc +def log_level_Enum_to_python_logging(log_level: LogLevel) -> int: + """ + Translates the custom LogLevel enum into logging module levels. + + Args: + log_level (LogLevel): The log level from the custom enum. + + Returns: + int: Corresponding logging module level. + """ + level_mapping = { + LogLevel.DEBUG: logging.DEBUG, + LogLevel.INFO: logging.INFO, + LogLevel.WARNING: logging.WARNING, + LogLevel.ERROR: logging.ERROR, + LogLevel.CRITICAL: logging.CRITICAL, + } + + # Return the corresponding logging level, defaulting to WARNING if not found + return level_mapping.get(log_level, logging.WARNING) + + def parse_arguments(config_str: str = "") -> Config: default_cfg: Config = Config() @@ -216,16 +256,42 @@ def parse_arguments(config_str: str = "") -> Config: timestamp = None # if hasattr(args, 'manifest'): - # logger.info(f"Manifest File Path: {args.manifest}") + # logger.debug(f"Manifest File Path: {args.manifest}") # manifest = args.manifest # else: # manifest = None # print the default configuration with all nested members # print(default_cfg) # TODO remove - global logger + # self.logger.setLevel(LogLevel.DEBUG) + + + logger2 = logger + #set the log level or proxypy and other modules #TODO make configurable via cmd line parameter + if (True): + logging.basicConfig( + level=log_level_Enum_to_python_logging(LogLevel.INFO), format="%(asctime)s - |%(name)s| %(levelname)s - %(message)s" + ) + + #set the log level of the time machine independently + formatter = logging.Formatter("%(asctime)s ||| %(name)s ||| - %(levelname)s - %(message)s") + if not logger.handlers: + handler = logging.StreamHandler() + handler.setFormatter(formatter) + logger.addHandler(handler) + logger.propagate = False # Prevent the logger from propagating to the root logger otherwise it will print the log messages twice + # else: + # # If handlers exist, apply the formatter to all handlers + # for handler in logger.handlers: + # handler.setFormatter(formatter) + + # global logger2 #global var seems to construct a logger object with another id which is weird seems to lead to issues with setting the log levels + #logger2 = logging.getLogger("ontologytimemachine.utils.config") + #_print_logger_info("before: config.py->parse_arguments getLogger(ontologytimemachine.utils.config)",logger2) + if args.logLevel == LogLevel.DEBUG: - logger.setLevel(logging.DEBUG) + logger.setLevel(log_level_Enum_to_python_logging(LogLevel.DEBUG)) + #_print_logger_info("after: config.py->parse_arguments getLogger(ontologytimemachine.utils.config)",logger2) logger.debug(f"Logging level set to: {args.logLevel}") elif args.logLevel == LogLevel.WARNING: logger.setLevel(logging.WARNING)