Working on logging
This commit is contained in:
parent
9d17178012
commit
c0769ad0b1
@ -29,9 +29,10 @@ import yaml
|
|||||||
|
|
||||||
class BackupRotator:
|
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.__dry_run = False
|
||||||
self.__configs = []
|
self.__configs = []
|
||||||
@ -64,6 +65,8 @@ class BackupRotator:
|
|||||||
now_s = now.strftime("%b-%d-%Y %I:%M%p")
|
now_s = now.strftime("%b-%d-%Y %I:%M%p")
|
||||||
return str(now_s)
|
return str(now_s)
|
||||||
|
|
||||||
|
def debug(self, s):
|
||||||
|
self.__logger.debug(s)
|
||||||
def info(self, s):
|
def info(self, s):
|
||||||
self.__logger.info(s)
|
self.__logger.info(s)
|
||||||
def warn(self, s):
|
def warn(self, s):
|
||||||
@ -73,7 +76,7 @@ class BackupRotator:
|
|||||||
|
|
||||||
def _consume_configs(self, paths: list=None):
|
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:")
|
||||||
print(configs)
|
print(configs)
|
||||||
return
|
return
|
||||||
@ -133,7 +136,7 @@ class BackupRotator:
|
|||||||
))
|
))
|
||||||
|
|
||||||
children = self._gather_rotation_candidates(config, path)
|
children = self._gather_rotation_candidates(config, path)
|
||||||
|
|
||||||
minimum_items = self._determine_minimum_items(config)
|
minimum_items = self._determine_minimum_items(config)
|
||||||
|
|
||||||
# Do we need to rotate anything out?
|
# Do we need to rotate anything out?
|
||||||
|
@ -12,17 +12,19 @@ class Config:
|
|||||||
"yml"
|
"yml"
|
||||||
]
|
]
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, logger):
|
||||||
|
|
||||||
self.__logger = Logger(type(self).__name__)
|
self.__logger = logger
|
||||||
self.__valid_extensions = self.__DEFAULT_VALID_EXTENSIONS
|
self.__valid_extensions = self.__DEFAULT_VALID_EXTENSIONS
|
||||||
|
|
||||||
|
def debug(self, s):
|
||||||
|
self.__logger.debug(f"[{type(self).__name__}] {s}")
|
||||||
def info(self, s):
|
def info(self, s):
|
||||||
self.__logger.info(s)
|
self.__logger.info(f"[{type(self).__name__}] {s}")
|
||||||
def warn(self, s):
|
def warn(self, s):
|
||||||
self.__logger.warn(s)
|
self.__logger.warn(f"[{type(self).__name__}] {s}")
|
||||||
def error(self, s):
|
def error(self, s):
|
||||||
self.__logger.error(s)
|
self.__logger.error(f"[{type(self).__name__}] {s}")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_dir_files_recursive(path: str):
|
def get_dir_files_recursive(path: str):
|
||||||
@ -45,7 +47,7 @@ class Config:
|
|||||||
assert paths is not None, "Config paths cannot be None"
|
assert paths is not None, "Config paths cannot be None"
|
||||||
assert len(paths) > 0, "Must provide at least one config file path"
|
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 = []
|
file_paths = []
|
||||||
configs = []
|
configs = []
|
||||||
@ -54,16 +56,16 @@ class Config:
|
|||||||
# First gather all files that are potential configs
|
# First gather all files that are potential configs
|
||||||
for path in paths:
|
for path in paths:
|
||||||
|
|
||||||
self.__logger.info(f"Inspecting path: {path}")
|
self.info(f"Inspecting path: {path}")
|
||||||
|
|
||||||
if os.path.isfile(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)
|
file_paths.append(path)
|
||||||
|
|
||||||
elif os.path.isdir(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):
|
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)
|
file_paths.append(file_path)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
@ -76,19 +78,19 @@ class Config:
|
|||||||
else:
|
else:
|
||||||
not_configs.append(file_path)
|
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:
|
if len(not_configs) > 0:
|
||||||
for not_config in not_configs:
|
for not_config in not_configs:
|
||||||
self.__logger.info(f"> {not_config}")
|
self.info(f"> {not_config}")
|
||||||
else:
|
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:
|
if len(configs) > 0:
|
||||||
for config in configs:
|
for config in configs:
|
||||||
self.__logger.info(f"> {config}")
|
self.info(f"> {config}")
|
||||||
else:
|
else:
|
||||||
self.__logger.info("> [none]")
|
self.info("> [none]")
|
||||||
|
|
||||||
return configs
|
return configs
|
||||||
|
|
||||||
|
@ -1,18 +1,35 @@
|
|||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
import sys
|
||||||
|
|
||||||
class Logger:
|
class Logger:
|
||||||
|
|
||||||
def __init__(self, name: str):
|
def __init__(self, name: str, debug: bool=False):
|
||||||
|
|
||||||
self.__name = name
|
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):
|
def debug(self, s):
|
||||||
print(self.__name, s)
|
self.__logger.debug(s)
|
||||||
def info(self, s):
|
def info(self, s):
|
||||||
print(self.__name, s)
|
self.__logger.info(s)
|
||||||
def warn(self, s):
|
def warn(self, s):
|
||||||
print(self.__name, s)
|
self.__logger.warn(s)
|
||||||
def error(self, s):
|
def error(self, s):
|
||||||
print(self.__name, s)
|
self.__logger.error(s)
|
||||||
|
|
||||||
|
12
main.py
12
main.py
@ -13,6 +13,14 @@ def main():
|
|||||||
description="Mike's Backup Rotator. Helps automatically remove old backup files or folders."
|
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(
|
parser.add_argument(
|
||||||
"--config", "-c",
|
"--config", "-c",
|
||||||
dest="config_files",
|
dest="config_files",
|
||||||
@ -31,7 +39,9 @@ def main():
|
|||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
rotator = BackupRotator()
|
rotator = BackupRotator(
|
||||||
|
debug=args.debug
|
||||||
|
)
|
||||||
rotator.run(
|
rotator.run(
|
||||||
configs=args.config_files,
|
configs=args.config_files,
|
||||||
dry_run=args.dry_run
|
dry_run=args.dry_run
|
||||||
|
Loading…
Reference in New Issue
Block a user