Skip to content

Commit

Permalink
first temporary PoC fix of logging setup
Browse files Browse the repository at this point in the history
  • Loading branch information
JJ-Author committed Oct 23, 2024
1 parent 2a9452b commit f7c7b5e
Showing 1 changed file with 73 additions and 7 deletions.
80 changes: 73 additions & 7 deletions ontologytimemachine/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f7c7b5e

Please sign in to comment.