Supress client debug messages

cliff sets log level to DEBUG in `run` function [1], need to overwrite
this configuration to depress DEBUG messages. This patch adds such
configuration in `prepare_to_run_command` function which will be called
by cliff after it sets log level to DEBUG [2][3].

[1] https://github.com/openstack/cliff/blob/master/cliff/app.py#L248
[2] https://github.com/openstack/cliff/blob/master/cliff/app.py#L279
[3] https://github.com/openstack/cliff/blob/master/cliff/app.py#L393

Change-Id: I5c270d74cb09e90b8a98fd8febf587b98db2306f
Closes-bug: #1681966
This commit is contained in:
Jeremy Liu 2018-03-16 17:09:35 +08:00
parent d0bf6c406e
commit a6ac975f77
1 changed files with 30 additions and 0 deletions

View File

@ -46,6 +46,13 @@ _IDENTITY_API_VERSION_3 = ['3']
class Barbican(app.App):
"""Barbican command line interface."""
# verbose logging levels
WARNING_LEVEL = 0
INFO_LEVEL = 1
DEBUG_LEVEL = 2
CONSOLE_MESSAGE_FORMAT = '%(message)s'
DEBUG_MESSAGE_FORMAT = '%(levelname)s: %(name)s %(message)s'
def __init__(self, **kwargs):
self.client = None
@ -328,6 +335,9 @@ class Barbican(app.App):
"""
self.client_manager = namedtuple('ClientManager', 'key_manager')
if cmd.auth_required:
# NOTE(liujiong): cliff sets log level to DEBUG in run function,
# need to overwrite this configuration to depress DEBUG messages.
self.configure_logging()
self.client_manager.key_manager = self.create_client(self.options)
def run(self, argv):
@ -337,6 +347,26 @@ class Barbican(app.App):
return 1
return super(Barbican, self).run(argv)
def configure_logging(self):
"""Create logging handlers for any log output."""
root_logger = logging.getLogger('')
# Set log level to INFO
root_logger.setLevel(logging.INFO)
# Send higher-level messages to the console via stderr
console = logging.StreamHandler(self.stderr)
console_level = {self.WARNING_LEVEL: logging.WARNING,
self.INFO_LEVEL: logging.INFO,
self.DEBUG_LEVEL: logging.DEBUG,
}.get(self.options.verbose_level, logging.INFO)
if logging.DEBUG == console_level:
formatter = logging.Formatter(self.DEBUG_MESSAGE_FORMAT)
else:
formatter = logging.Formatter(self.CONSOLE_MESSAGE_FORMAT)
console.setFormatter(formatter)
root_logger.addHandler(console)
return
def main(argv=sys.argv[1:]):
logging.basicConfig()