Define better warning facility.
This commit is contained in:
parent
01ca5910ec
commit
c63ab44c9c
6 changed files with 68 additions and 17 deletions
|
@ -20,9 +20,9 @@
|
|||
#
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
import hashlib
|
||||
|
||||
import cdist.log
|
||||
import cdist.version
|
||||
|
||||
VERSION = cdist.version.VERSION
|
||||
|
|
|
@ -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
|
||||
|
|
35
cdist/log.py
35
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')
|
||||
|
|
|
@ -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 = ''
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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])):
|
||||
|
|
Loading…
Reference in a new issue