From c63ab44c9cefcedd355d6952677d3cdab9ac61fe Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Wed, 28 Jun 2017 10:18:53 +0200 Subject: [PATCH] Define better warning facility. --- cdist/__init__.py | 2 +- cdist/argparse.py | 31 +++++++++++++++++++++++-------- cdist/log.py | 35 +++++++++++++++++++++++++++++++++++ cdist/util/ipaddr.py | 4 ++-- docs/src/man1/cdist.rst | 10 +++++++--- scripts/cdist | 3 --- 6 files changed, 68 insertions(+), 17 deletions(-) diff --git a/cdist/__init__.py b/cdist/__init__.py index b0fd75ea..d92205a6 100644 --- a/cdist/__init__.py +++ b/cdist/__init__.py @@ -20,9 +20,9 @@ # import os -import subprocess import hashlib +import cdist.log import cdist.version VERSION = cdist.version.VERSION diff --git a/cdist/argparse.py b/cdist/argparse.py index 3accedeb..8689be79 100644 --- a/cdist/argparse.py +++ b/cdist/argparse.py @@ -17,13 +17,20 @@ EPILOG = "Get cdist at http://www.nico.schottelius.org/software/cdist/" parser = None +_verbosity_level_off = -2 +_verbosity_level_debug = 3 _verbosity_level = { - 0: logging.ERROR, - 1: logging.WARNING, - 2: logging.INFO, + _verbosity_level_off: logging.OFF, + -1: logging.ERROR, + 0: logging.WARNING, + 1: logging.INFO, + 2: logging.VERBOSE, + 3: logging.DEBUG, + 4: logging.TRACE, } +# All verbosity levels above 4 are TRACE. _verbosity_level = collections.defaultdict( - lambda: logging.DEBUG, _verbosity_level) + lambda: logging.TRACE, _verbosity_level) def add_beta_command(cmd): @@ -83,13 +90,18 @@ def get_parsers(): '-d', '--debug', help=('Set log level to debug (deprecated, use -vvv instead)'), action='store_true', default=False) + parser['loglevel'].add_argument( + '-q', '--quiet', + help='Quiet mode: disables logging, including WARNING and ERROR', + action='store_true', default=False) parser['loglevel'].add_argument( '-v', '--verbose', help=('Increase the verbosity level. Every instance of -v ' 'increments the verbosity level by one. Its default value ' - 'is 0. There are 4 levels of verbosity. The order of levels ' - 'from the lowest to the highest are: ERROR (0), ' - 'WARNING (1), INFO (2) and DEBUG (3 or higher).'), + 'is 0 which includes ERROR and WARNING levels. ' + 'The levels, in order from the lowest to the highest, are: ' + 'ERROR (-1), WARNING (0), INFO (1), VERBOSE (2), DEBUG (3) ' + 'TRACE (4 or higher).'), action='count', default=0) parser['beta'] = argparse.ArgumentParser(add_help=False) @@ -213,10 +225,13 @@ def get_parsers(): def handle_loglevel(args): if args.debug: retval = "-d/--debug is deprecated, use -vvv instead" - args.verbose = 3 + args.verbose = _verbosity_level_debug else: retval = None + if args.quiet: + args.verbose = _verbosity_level_off + logging.root.setLevel(_verbosity_level[args.verbose]) return retval diff --git a/cdist/log.py b/cdist/log.py index 2341c282..ce0addcc 100644 --- a/cdist/log.py +++ b/cdist/log.py @@ -23,6 +23,31 @@ import logging +# Define additional cdist logging levels. +logging.OFF = logging.CRITICAL + 10 # disable logging +logging.addLevelName(logging.OFF, 'OFF') + +logging.VERBOSE = logging.INFO - 5 +logging.addLevelName(logging.VERBOSE, 'VERBOSE') + + +def _verbose(msg, *args, **kwargs): + logging.log(logging.VERBOSE, msg, *args, **kwargs) + + +logging.verbose = _verbose + +logging.TRACE = logging.DEBUG - 5 +logging.addLevelName(logging.TRACE, 'TRACE') + + +def _trace(msg, *args, **kwargs): + logging.log(logging.TRACE, msg, *args, **kwargs) + + +logging.trace = _trace + + class Log(logging.Logger): def __init__(self, name): @@ -37,3 +62,13 @@ class Log(logging.Logger): record.msg = self.name + ": " + str(record.msg) return True + + def verbose(self, msg, *args, **kwargs): + self.log(logging.VERBOSE, msg, *args, **kwargs) + + def trace(self, msg, *args, **kwargs): + self.log(logging.TRACE, msg, *args, **kwargs) + + +logging.setLoggerClass(Log) +logging.basicConfig(format='%(levelname)s: %(message)s') diff --git a/cdist/util/ipaddr.py b/cdist/util/ipaddr.py index a747aeb5..b9d1ab30 100644 --- a/cdist/util/ipaddr.py +++ b/cdist/util/ipaddr.py @@ -45,7 +45,7 @@ def resolve_target_host_name(host): log.debug("derived host_name for host \"{}\": {}".format( host, host_name)) except (socket.gaierror, socket.herror) as e: - log.warn("Could not derive host_name for {}" + log.warning("Could not derive host_name for {}" ", $host_name will be empty. Error is: {}".format(host, e)) # in case of error provide empty value host_name = '' @@ -59,7 +59,7 @@ def resolve_target_fqdn(host): log.debug("derived host_fqdn for host \"{}\": {}".format( host, host_fqdn)) except socket.herror as e: - log.warn("Could not derive host_fqdn for {}" + log.warning("Could not derive host_fqdn for {}" ", $host_fqdn will be empty. Error is: {}".format(host, e)) # in case of error provide empty value host_fqdn = '' diff --git a/docs/src/man1/cdist.rst b/docs/src/man1/cdist.rst index 3bddbd02..dbfda34f 100644 --- a/docs/src/man1/cdist.rst +++ b/docs/src/man1/cdist.rst @@ -50,12 +50,16 @@ All commands accept the following options: Set log level to debug (deprecated, use -vvv instead) +.. option:: -q, --quiet + + Quiet mode: disables logging, including WARNING and ERROR + .. option:: -v, --verbose Increase the verbosity level. Every instance of -v increments the verbosity - level by one. Its default value is 0. There are 4 levels of verbosity. The - order of levels from the lowest to the highest are: ERROR (0), WARNING (1), - INFO (2) and DEBUG (3 or higher). + level by one. Its default value is 0 which includes ERROR and WARNING levels. + The levels, in order from the lowest to the highest, are: + ERROR (-1), WARNING (0), INFO (1), VERBOSE (2), DEBUG (3) TRACE (4 or higher). .. option:: -V, --version diff --git a/scripts/cdist b/scripts/cdist index 498091b8..9719e6ee 100755 --- a/scripts/cdist +++ b/scripts/cdist @@ -80,10 +80,7 @@ if __name__ == "__main__": import os import re import cdist - import cdist.log - logging.setLoggerClass(cdist.log.Log) - logging.basicConfig(format='%(levelname)s: %(message)s') log = logging.getLogger("cdist") if re.match("__", os.path.basename(sys.argv[0])):