diff --git a/repo_cloner/initialization_wizard.py b/repo_cloner/initialization_wizard.py index a0e94da..65687bc 100644 --- a/repo_cloner/initialization_wizard.py +++ b/repo_cloner/initialization_wizard.py @@ -33,8 +33,10 @@ def parse_args(): import argparse # parse input arguments parser = argparse.ArgumentParser(description = "repo-cloner initialization wizzard") - parser.add_argument('--base-dir', help = 'path to directory containing whole cloner structure', required = True, - default = None, type = str) + parser.add_argument( + '--base-dir', help = 'path to directory containing whole cloner structure', required = True, + default = None, type = str) + parser.add_argument('--colored', help = "enable colored log output even tty() is not detected") args = parser.parse_args() base_dir = Path(args.base_dir) @@ -69,7 +71,7 @@ def gen_config_file( "# cloner interval (in minutes, default=0 - run always)\n" f"cloner_interval = {cloner_interval}\n\n" "# do you need submodules support? (1/0)\n" - f"cloner_submodules = {cloner_submodules}\n\n" + f"cloner_submodules = {1 if cloner_submodules else 0}\n\n" "# max depth of submodule scan (default = unlimited, uncomment to use)\n" f"{'' if cloner_submodules else '# '}cloner_submodule_depth = {cloner_submodule_depth}\n\n" ) diff --git a/repo_cloner/lib/logger_setup.py b/repo_cloner/lib/logger_setup.py index 61379c5..b92349b 100644 --- a/repo_cloner/lib/logger_setup.py +++ b/repo_cloner/lib/logger_setup.py @@ -1,9 +1,43 @@ import logging +import sys + + +class CustomFormatter(logging.Formatter): + grey = "\x1b[38;20m" + light_blue = "\x1b[34;1m" + yellow = "\x1b[33;20m" + red = "\x1b[31;20m" + bold_red = "\x1b[31;1m" + reset = "\x1b[0m" + # format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)" + format = "%(asctime)-15s.%(msecs)03d :: [%(levelname)8s] :: %(name)-15s :: %(message)s (%(filename)s:%(lineno)s)" + time_format = "%Y-%m-%d %H:%M:%S" + + FORMATS = { + logging.DEBUG: grey + format + reset, + logging.INFO: light_blue + format + reset, + logging.WARNING: yellow + format + reset, + logging.ERROR: red + format + reset, + logging.CRITICAL: bold_red + format + reset + } + + def format(self, record): + log_fmt = self.FORMATS.get(record.levelno) + formatter = logging.Formatter(log_fmt, self.time_format) + return formatter.format(record) + console_logger = logging.StreamHandler() -console_formatter = logging.Formatter( - "%(asctime)-15s.%(msecs)03d :: [%(levelname)8s] :: %(name)-15s :: %(message)s (%(filename)s:%(lineno)s)", - "%Y-%m-%d %H:%M:%S") + +# formatter for stdout - colored +if sys.stdout.isatty() or "--colored" in sys.argv: + console_formatter = CustomFormatter() +else: + # b&w for rest + console_formatter = logging.Formatter( + "%(asctime)-15s.%(msecs)03d :: [%(levelname)8s] :: %(name)-15s :: %(message)s (%(filename)s:%(lineno)s)", + "%Y-%m-%d %H:%M:%S") + # setup logger console_logger.setFormatter(console_formatter) log = logging.getLogger("rc") diff --git a/repo_cloner/process_repository_dir.py b/repo_cloner/process_repository_dir.py index f89dd3b..badff56 100755 --- a/repo_cloner/process_repository_dir.py +++ b/repo_cloner/process_repository_dir.py @@ -90,6 +90,7 @@ def main() -> int: parser.add_argument('--base-dir', help = 'path to directory containing whole cloner structure', required = True, default = None, type = str) parser.add_argument('--debug', '-d', help = "enable debug output", action = 'store_true') + parser.add_argument('--colored', help = "enable colored log output even tty() is not detected") args = parser.parse_args() if args.debug: log.setLevel(logging.DEBUG)