From 6a3385e9c0d031740e061b36c00dd341990a3410 Mon Sep 17 00:00:00 2001 From: Scott Gasch Date: Sun, 5 Mar 2023 20:36:39 -0800 Subject: [PATCH] Fix clear_preexisting_handers functionality. --- src/pyutils/logging_utils.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/pyutils/logging_utils.py b/src/pyutils/logging_utils.py index 8011604..e1f3a22 100644 --- a/src/pyutils/logging_utils.py +++ b/src/pyutils/logging_utils.py @@ -921,20 +921,19 @@ def initialize_logging(logger=None) -> logging.Logger: return logging.getLogger() LOGGING_INITIALIZED = True + clear_preexisting = config.config["logging_clear_preexisting_handlers"] preexisting_handlers_count = 0 if logger is None: - # --logging_clear_preexisting_handlers removes logging handlers - # that were registered by global statements during imported module - # setup. - if config.config["logging_clear_preexisting_handlers"]: - logging.basicConfig(force=True) - logger = logging.getLogger() - while logger.hasHandlers(): - logger.removeHandler(logger.handlers[0]) - preexisting_handlers_count += 1 - else: - logging.basicConfig() - logger = logging.getLogger() + logging.basicConfig(force=clear_preexisting) + logger = logging.getLogger() + + # --logging_clear_preexisting_handlers removes logging handlers + # that were registered by global statements during imported module + # setup. + if clear_preexisting: + while logger.hasHandlers(): + logger.removeHandler(logger.handlers[0]) + preexisting_handlers_count += 1 # --logging_config_file pulls logging settings from a config file # skipping the rest of this setup. -- 2.47.1