From 1063ea147e637d1f4103d4382df0568d66086749 Mon Sep 17 00:00:00 2001 From: karnigen Date: Sat, 23 Mar 2024 18:43:06 +0100 Subject: [PATCH] updated debug logger [no ci] --- LOGGING_template.toml | 4 ++-- lib/debug/debug.py | 28 +++++++++++++++++----------- lib/debug/logging.py | 8 ++++---- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/LOGGING_template.toml b/LOGGING_template.toml index 27aadcb3b..85e3d2a9b 100644 --- a/LOGGING_template.toml +++ b/LOGGING_template.toml @@ -56,9 +56,9 @@ disable_existing_loggers = false ### used for: logger = logging.getLogger("inkstitch.debug") ### - use quotes for the logger name with dots, otherwise it will be treated as a table subsection ### - [loggers.inkstitch.debug] is not the same as [loggers.'inkstitch.debug'] -### - to disable the logger, comment out handlers [loggers.'inkstitch.debug'] - level = "DEBUG" + level = "DEBUG" # to enable the logger, seems to be the default + # level = "CRITICAL" # to disable the logger handlers = [ "file_inkstitch_debug",] propagate = false diff --git a/lib/debug/debug.py b/lib/debug/debug.py index 69034c4e6..ab5132e4e 100644 --- a/lib/debug/debug.py +++ b/lib/debug/debug.py @@ -79,15 +79,24 @@ class Debug(object): self.last_log_time = None self.current_layer = None self.group_stack = [] + self.svg_filename = None def enable(self): + # determine svg filename from logger + if len(logger.handlers) > 0 and isinstance(logger.handlers[0], logging.FileHandler): + # determine filename of svg file from logger + filename = Path(logger.handlers[0].baseFilename) + self.svg_filename = filename.with_suffix(".svg") + self.svg_filename.unlink(missing_ok=True) # remove existing svg file + # self.log is activated by active logger - # - enabled only if logger has any handler, - # for "inkstitch.debug" simply comment out handlers in .toml file to disable for logger - if len(logger.handlers) > 0: # count of handlers + # - enabled only if logger first handler is FileHandler + # to disable "inkstitch.debug" simply set logging level to CRITICAL + if logger.isEnabledFor(logging.INFO) and self.svg_filename is not None: self.enabled = True - self.log("Logging enabled") + self.log(f"Logging enabled with svg file: {self.svg_filename}") self.init_svg() + else: # use alternative logger to log message if logger has no handlers logger_inkstich.info("No handlers in logger, cannot enable logging and svg file creation") @@ -97,16 +106,13 @@ class Debug(object): atexit.register(self.save_svg) def save_svg(self): - # check if there is a file handler and is type of logging.FileHandler - if len(logger.handlers) > 0 and isinstance(logger.handlers[0], logging.FileHandler): - filename = Path(logger.handlers[0].baseFilename) - svg_file = filename.with_suffix(".svg") - self.log(f"Writing svg file: {svg_file}") + if self.enabled and self.svg_filename is not None: + self.log(f"Writing svg file: {self.svg_filename}") tree = etree.ElementTree(self.svg) - tree.write(str(svg_file)) # lxml <5.0.0 does not support Path objects + tree.write(str(self.svg_filename)) # lxml <5.0.0 does not support Path objects, requires string else: # use alternative logger to log message if logger has no handlers - logger_inkstich.info("No file handler in logger cannot save svg file") + logger_inkstich.info(f"Saving to svg file is not activated {self.svg_filename=}") @check_enabled @unwrap_arguments diff --git a/lib/debug/logging.py b/lib/debug/logging.py index 81029ff9b..68557975e 100644 --- a/lib/debug/logging.py +++ b/lib/debug/logging.py @@ -102,7 +102,7 @@ def activate_for_frozen(): if docpath is not None and loglevel is not None and loglevel.upper() in ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL']: - # end user enabled logging & warnings are redirect output to input_svg.inkstitch.log + # The end user enabled logging and warnings are redirected to the input_svg.inkstitch.log file. vars = { 'loglevel': loglevel.upper(), @@ -113,7 +113,7 @@ def activate_for_frozen(): # dictConfig has access to top level variables, dict contains: ext://__main__.var # - restriction: variable must be last token in string - very limited functionality, avoid using it - # here is logging activated, so we can use logger + # After this operation, logging will be activated, so we can use the logger. logging.config.dictConfig(config) # configure root logger from dict logging.captureWarnings(True) # capture all warnings to log file with level WARNING @@ -153,7 +153,7 @@ def activate_for_development(ini: dict, SCRIPTDIR: Path): def configure_logging(config: dict, ini: dict, vars: dict): config = expand_variables(config, vars) - # here is logging activated, so now we can use logger + # After this operation, logging will be activated, so we can use the logger. logging.config.dictConfig(config) # configure loggers from dict - using loglevel, logfilename warnings_capture = config.get('warnings_capture', True) @@ -163,7 +163,7 @@ def configure_logging(config: dict, ini: dict, vars: dict): disable_logging = safe_get(ini, "LOGGING", "disable_logging", default=False) if disable_logging: - logger.warning("Logging is disabled by configuration in ini file.") + logger.warning(f"Logging is disabled by configuration in ini file. {disable_logging = }") logging.disable() # globally disable all logging of all loggers