Move options to log level out of shell.py
Move the conversion of command line options to log level out of shell.py. Change-Id: I86cb45a85cd63927aa1c87c1eed27542981df659 Implements: blueprint logging-migration
This commit is contained in:
parent
9c3c336391
commit
ca9965c328
openstackclient
@ -22,6 +22,22 @@ _LOG_MESSAGE_FORMAT = ('%(asctime)s.%(msecs)03d %(process)d '
|
|||||||
_LOG_DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
|
_LOG_DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
|
||||||
|
|
||||||
|
|
||||||
|
def log_level_from_options(options):
|
||||||
|
# if --debug, --quiet or --verbose is not specified,
|
||||||
|
# the default logging level is warning
|
||||||
|
log_level = logging.WARNING
|
||||||
|
if options.verbose_level == 0:
|
||||||
|
# --quiet
|
||||||
|
log_level = logging.ERROR
|
||||||
|
elif options.verbose_level == 2:
|
||||||
|
# One --verbose
|
||||||
|
log_level = logging.INFO
|
||||||
|
elif options.verbose_level >= 3:
|
||||||
|
# Two or more --verbose
|
||||||
|
log_level = logging.DEBUG
|
||||||
|
return log_level
|
||||||
|
|
||||||
|
|
||||||
def set_warning_filter(log_level):
|
def set_warning_filter(log_level):
|
||||||
if log_level == logging.ERROR:
|
if log_level == logging.ERROR:
|
||||||
warnings.simplefilter("ignore")
|
warnings.simplefilter("ignore")
|
||||||
|
@ -113,18 +113,7 @@ class OpenStackShell(app.App):
|
|||||||
super(OpenStackShell, self).configure_logging()
|
super(OpenStackShell, self).configure_logging()
|
||||||
|
|
||||||
# Set logging to the requested level
|
# Set logging to the requested level
|
||||||
if self.options.verbose_level == 0:
|
log_level = context.log_level_from_options(self.options)
|
||||||
# --quiet
|
|
||||||
log_level = logging.ERROR
|
|
||||||
elif self.options.verbose_level == 1:
|
|
||||||
# This is the default case, no --debug, --verbose or --quiet
|
|
||||||
log_level = logging.WARNING
|
|
||||||
elif self.options.verbose_level == 2:
|
|
||||||
# One --verbose
|
|
||||||
log_level = logging.INFO
|
|
||||||
elif self.options.verbose_level >= 3:
|
|
||||||
# Two or more --verbose
|
|
||||||
log_level = logging.DEBUG
|
|
||||||
context.set_warning_filter(log_level)
|
context.set_warning_filter(log_level)
|
||||||
|
|
||||||
# Set the handler logging level of FileHandler(--log-file)
|
# Set the handler logging level of FileHandler(--log-file)
|
||||||
|
@ -62,6 +62,17 @@ class TestContext(utils.TestCase):
|
|||||||
context.setup_logging(shell, cloud_config)
|
context.setup_logging(shell, cloud_config)
|
||||||
self.assertEqual(True, shell.enable_operation_logging)
|
self.assertEqual(True, shell.enable_operation_logging)
|
||||||
|
|
||||||
|
def test_log_level_from_options(self):
|
||||||
|
opts = mock.Mock()
|
||||||
|
opts.verbose_level = 0
|
||||||
|
self.assertEqual(logging.ERROR, context.log_level_from_options(opts))
|
||||||
|
opts.verbose_level = 1
|
||||||
|
self.assertEqual(logging.WARNING, context.log_level_from_options(opts))
|
||||||
|
opts.verbose_level = 2
|
||||||
|
self.assertEqual(logging.INFO, context.log_level_from_options(opts))
|
||||||
|
opts.verbose_level = 3
|
||||||
|
self.assertEqual(logging.DEBUG, context.log_level_from_options(opts))
|
||||||
|
|
||||||
@mock.patch('warnings.simplefilter')
|
@mock.patch('warnings.simplefilter')
|
||||||
def test_set_warning_filter(self, simplefilter):
|
def test_set_warning_filter(self, simplefilter):
|
||||||
context.set_warning_filter(logging.ERROR)
|
context.set_warning_filter(logging.ERROR)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user