From c0769ad0b16fc52b014a8bd6ddbb6210e3718cce Mon Sep 17 00:00:00 2001 From: root Date: Mon, 27 Mar 2023 19:59:45 -0700 Subject: [PATCH] Working on logging --- domain/BackupRotator.py | 11 +++++++---- domain/Config.py | 36 +++++++++++++++++++----------------- domain/Logger.py | 29 +++++++++++++++++++++++------ main.py | 12 +++++++++++- 4 files changed, 60 insertions(+), 28 deletions(-) diff --git a/domain/BackupRotator.py b/domain/BackupRotator.py index 0d1bc70..c7a28bd 100755 --- a/domain/BackupRotator.py +++ b/domain/BackupRotator.py @@ -29,9 +29,10 @@ import yaml class BackupRotator: - def __init__(self): + def __init__(self, debug:bool = False): - self.__logger = Logger(type(self).__name__) + self.__logger = Logger(name=type(self).__name__, debug=debug) + self.__config_helper = Config(logger=self.__logger) self.__dry_run = False self.__configs = [] @@ -64,6 +65,8 @@ class BackupRotator: now_s = now.strftime("%b-%d-%Y %I:%M%p") return str(now_s) + def debug(self, s): + self.__logger.debug(s) def info(self, s): self.__logger.info(s) def warn(self, s): @@ -73,7 +76,7 @@ class BackupRotator: def _consume_configs(self, paths: list=None): - configs = Config().gather_valid_configs(paths=paths) + configs = self.__config_helper.gather_valid_configs(paths=paths) print("Configs:") print(configs) return @@ -133,7 +136,7 @@ class BackupRotator: )) children = self._gather_rotation_candidates(config, path) - + minimum_items = self._determine_minimum_items(config) # Do we need to rotate anything out? diff --git a/domain/Config.py b/domain/Config.py index f02425d..4a002ed 100644 --- a/domain/Config.py +++ b/domain/Config.py @@ -12,17 +12,19 @@ class Config: "yml" ] - def __init__(self): + def __init__(self, logger): - self.__logger = Logger(type(self).__name__) + self.__logger = logger self.__valid_extensions = self.__DEFAULT_VALID_EXTENSIONS + def debug(self, s): + self.__logger.debug(f"[{type(self).__name__}] {s}") def info(self, s): - self.__logger.info(s) + self.__logger.info(f"[{type(self).__name__}] {s}") def warn(self, s): - self.__logger.warn(s) + self.__logger.warn(f"[{type(self).__name__}] {s}") def error(self, s): - self.__logger.error(s) + self.__logger.error(f"[{type(self).__name__}] {s}") @staticmethod def get_dir_files_recursive(path: str): @@ -45,7 +47,7 @@ class Config: assert paths is not None, "Config paths cannot be None" assert len(paths) > 0, "Must provide at least one config file path" - self.__logger.info("Gathering valid configs") + self.info("Gathering valid configs") file_paths = [] configs = [] @@ -54,16 +56,16 @@ class Config: # First gather all files that are potential configs for path in paths: - self.__logger.info(f"Inspecting path: {path}") + self.info(f"Inspecting path: {path}") if os.path.isfile(path): - self.__logger.info(f"Path is a file; Adding directly to potential config candidates: {path}") + self.debug(f"Path is a file; Adding directly to potential config candidates: {path}") file_paths.append(path) - + elif os.path.isdir(path): - self.__logger.info(f"Path is a dir; Scanning recursively for potential config candidate files: {path}") + self.debug(f"Path is a dir; Scanning recursively for potential config candidate files: {path}") for file_path in Config.get_dir_files_recursive(path=path): - self.__logger.info(f"> Candidate file: {file_path}") + self.info(f"> Candidate file: {file_path}") file_paths.append(file_path) else: @@ -76,19 +78,19 @@ class Config: else: not_configs.append(file_path) - self.__logger.info("Filtered out non-config files:") + self.info("Filtered out non-config files:") if len(not_configs) > 0: for not_config in not_configs: - self.__logger.info(f"> {not_config}") + self.info(f"> {not_config}") else: - self.__logger.info("> [none]") + self.info("> [none]") - self.__logger.info("Kept config-looking files:") + self.info("Kept config-looking files:") if len(configs) > 0: for config in configs: - self.__logger.info(f"> {config}") + self.info(f"> {config}") else: - self.__logger.info("> [none]") + self.info("> [none]") return configs diff --git a/domain/Logger.py b/domain/Logger.py index 5fad232..7f373f6 100644 --- a/domain/Logger.py +++ b/domain/Logger.py @@ -1,18 +1,35 @@ import logging +import sys class Logger: - def __init__(self, name: str): + def __init__(self, name: str, debug: bool=False): self.__name = name + + self.__logger = logging.getLogger(self.__name) + if debug: + level = logging.DEBUG + else: + level = logging.INFO + + self.__logger.setLevel(level) + + formatter = logging.Formatter('[%(asctime)s][%(name)s][%(levelname)s] %(message)s') + + # Console output / stream handler + handler = logging.StreamHandler() + handler.setLevel(level) + handler.setFormatter(formatter) + self.__logger.addHandler(handler) + def debug(self, s): - print(self.__name, s) + self.__logger.debug(s) def info(self, s): - print(self.__name, s) + self.__logger.info(s) def warn(self, s): - print(self.__name, s) + self.__logger.warn(s) def error(self, s): - print(self.__name, s) - + self.__logger.error(s) diff --git a/main.py b/main.py index 02e5da3..835bace 100755 --- a/main.py +++ b/main.py @@ -13,6 +13,14 @@ def main(): description="Mike's Backup Rotator. Helps automatically remove old backup files or folders." ) + parser.add_argument( + "--debug", "--verbose", + dest="debug", + default=False, + action="store_true", + help="Verbose/Debug logging mode" + ) + parser.add_argument( "--config", "-c", dest="config_files", @@ -31,7 +39,9 @@ def main(): args = parser.parse_args() - rotator = BackupRotator() + rotator = BackupRotator( + debug=args.debug + ) rotator.run( configs=args.config_files, dry_run=args.dry_run