From 41274c3239da60af44a6f31482bfca12006fe659 Mon Sep 17 00:00:00 2001 From: ldericher Date: Mon, 10 Aug 2020 14:39:28 +0200 Subject: [PATCH] logging verbosity --- src/kiwi-config.py | 30 ++++++++++++++++++------------ src/kiwi/core.py | 3 +-- src/kiwi/subcommands/init.py | 4 ++-- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/kiwi-config.py b/src/kiwi-config.py index f0da7c1..3e2c589 100755 --- a/src/kiwi-config.py +++ b/src/kiwi-config.py @@ -5,12 +5,22 @@ import kiwi from kiwi.subcommands import * -def main(): - logging.basicConfig( - format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", - level=logging.NOTSET - ) +def set_verbosity(logger, handler, verbosity): + if verbosity >= 2: + log_level = logging.DEBUG + log_format = "[%(asctime)s] %(levelname)s @ %(filename)s:%(funcName)s:%(lineno)d: %(message)s" + elif verbosity >= 1: + log_level = logging.INFO + log_format = "[%(asctime)s] %(levelname)s: %(message)s" + else: + log_level = logging.WARNING + log_format = "%(levelname)s: %(message)s" + logger.setLevel(log_level) + handler.setFormatter(logging.Formatter(log_format)) + + +def main(): commands = [ InitCommand, ShowCommand, @@ -22,14 +32,10 @@ def main(): args = kiwi.Parser.get_args() - if args.verbose >= 2: - log_level = logging.DEBUG - elif args.verbose >= 1: - log_level = logging.INFO - else: - log_level = logging.WARNING + log_handler = logging.StreamHandler() + logging.getLogger().addHandler(log_handler) - logging.getLogger().setLevel(log_level) + set_verbosity(logging.getLogger(), log_handler, args.verbosity) for cmd in commands: if cmd.command == args.command: diff --git a/src/kiwi/core.py b/src/kiwi/core.py index 1ded8e5..8de4feb 100644 --- a/src/kiwi/core.py +++ b/src/kiwi/core.py @@ -19,7 +19,7 @@ class Parser: cls.__parser = argparse.ArgumentParser(description='kiwi-config') cls.__parser.add_argument( - '-v', '--verbose', + '-v', '--verbosity', action='count', default=0 ) @@ -40,4 +40,3 @@ class Parser: cls.__args = cls.get_parser().parse_args() return cls.__args - diff --git a/src/kiwi/subcommands/init.py b/src/kiwi/subcommands/init.py index 429a073..b377545 100644 --- a/src/kiwi/subcommands/init.py +++ b/src/kiwi/subcommands/init.py @@ -37,11 +37,11 @@ def user_input_exe(config, key): program_name = key.split(':')[1] if not is_executable(exe_file): - logging.warning("Reconfiguring '%s' executable path.", program_name) + logging.info("Reconfiguring '%s' executable path.", program_name) exe_file = find_exe(program_name) if exe_file is not None: - logging.info("Found executable at '%s'.", exe_file) + logging.debug("Found executable at '%s'.", exe_file) config[key] = exe_file else: user_input(config, key, f"Enter path to '{program_name}' executable")