Merge "Create log configuration class"
This commit is contained in:
commit
8fe6120490
@ -94,60 +94,12 @@ class OpenStackShell(app.App):
|
|||||||
self.verify = True
|
self.verify = True
|
||||||
|
|
||||||
self.client_manager = None
|
self.client_manager = None
|
||||||
|
|
||||||
# Operation log
|
|
||||||
self.enable_operation_logging = False
|
|
||||||
self.command_options = None
|
self.command_options = None
|
||||||
|
|
||||||
def configure_logging(self):
|
def configure_logging(self):
|
||||||
"""Configure logging for the app
|
"""Configure logging for the app."""
|
||||||
|
self.log_configurator = context.LogConfigurator(self.options)
|
||||||
Cliff sets some defaults we don't want so re-work it a bit
|
self.dump_stack_trace = self.log_configurator.dump_trace
|
||||||
"""
|
|
||||||
|
|
||||||
if self.options.debug:
|
|
||||||
# --debug forces verbose_level 3
|
|
||||||
# Set this here so cliff.app.configure_logging() can work
|
|
||||||
self.options.verbose_level = 3
|
|
||||||
|
|
||||||
super(OpenStackShell, self).configure_logging()
|
|
||||||
|
|
||||||
# Set logging to the requested level
|
|
||||||
log_level = context.log_level_from_options(self.options)
|
|
||||||
context.set_warning_filter(log_level)
|
|
||||||
|
|
||||||
# Set the handler logging level of FileHandler(--log-file)
|
|
||||||
# and StreamHandler
|
|
||||||
if self.options.log_file:
|
|
||||||
context.setup_handler_logging_level(logging.FileHandler, log_level)
|
|
||||||
|
|
||||||
context.setup_handler_logging_level(logging.StreamHandler, log_level)
|
|
||||||
|
|
||||||
# Requests logs some stuff at INFO that we don't want
|
|
||||||
# unless we have DEBUG
|
|
||||||
requests_log = logging.getLogger("requests")
|
|
||||||
|
|
||||||
# Other modules we don't want DEBUG output for
|
|
||||||
cliff_log = logging.getLogger('cliff')
|
|
||||||
stevedore_log = logging.getLogger('stevedore')
|
|
||||||
iso8601_log = logging.getLogger("iso8601")
|
|
||||||
|
|
||||||
if self.options.debug:
|
|
||||||
# --debug forces traceback
|
|
||||||
self.dump_stack_trace = True
|
|
||||||
requests_log.setLevel(logging.DEBUG)
|
|
||||||
else:
|
|
||||||
self.dump_stack_trace = False
|
|
||||||
requests_log.setLevel(logging.ERROR)
|
|
||||||
|
|
||||||
cliff_log.setLevel(logging.ERROR)
|
|
||||||
stevedore_log.setLevel(logging.ERROR)
|
|
||||||
iso8601_log.setLevel(logging.ERROR)
|
|
||||||
|
|
||||||
# Operation logging
|
|
||||||
self.operation_log = logging.getLogger("operation_log")
|
|
||||||
self.operation_log.setLevel(logging.ERROR)
|
|
||||||
self.operation_log.propagate = False
|
|
||||||
|
|
||||||
def run(self, argv):
|
def run(self, argv):
|
||||||
ret_val = 1
|
ret_val = 1
|
||||||
@ -162,8 +114,6 @@ class OpenStackShell(app.App):
|
|||||||
self.log.error(traceback.format_exc(e))
|
self.log.error(traceback.format_exc(e))
|
||||||
else:
|
else:
|
||||||
self.log.error('Exception raised: ' + str(e))
|
self.log.error('Exception raised: ' + str(e))
|
||||||
if self.enable_operation_logging:
|
|
||||||
self.operation_log.error(traceback.format_exc(e))
|
|
||||||
|
|
||||||
return ret_val
|
return ret_val
|
||||||
|
|
||||||
@ -287,9 +237,8 @@ class OpenStackShell(app.App):
|
|||||||
argparse=self.options,
|
argparse=self.options,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Set up every time record log in file and logging start
|
self.log_configurator.configure(self.cloud)
|
||||||
context.setup_logging(self, self.cloud)
|
self.dump_stack_trace = self.log_configurator.dump_trace
|
||||||
|
|
||||||
self.log.info("START with options: %s", self.command_options)
|
self.log.info("START with options: %s", self.command_options)
|
||||||
self.log.debug("options: %s", self.options)
|
self.log.debug("options: %s", self.options)
|
||||||
self.log.debug("defaults: %s", cc.defaults)
|
self.log.debug("defaults: %s", cc.defaults)
|
||||||
|
@ -625,12 +625,9 @@ class TestShellCli(TestShell):
|
|||||||
|
|
||||||
@mock.patch("os_client_config.config.OpenStackConfig._load_vendor_file")
|
@mock.patch("os_client_config.config.OpenStackConfig._load_vendor_file")
|
||||||
@mock.patch("os_client_config.config.OpenStackConfig._load_config_file")
|
@mock.patch("os_client_config.config.OpenStackConfig._load_config_file")
|
||||||
@mock.patch("openstackclient.common.context._setup_handler_for_logging")
|
def test_shell_args_cloud_public(self, config_mock, public_mock):
|
||||||
def test_shell_args_cloud_public(self, setup_handler, config_mock,
|
|
||||||
public_mock):
|
|
||||||
config_mock.return_value = ('file.yaml', copy.deepcopy(CLOUD_2))
|
config_mock.return_value = ('file.yaml', copy.deepcopy(CLOUD_2))
|
||||||
public_mock.return_value = ('file.yaml', copy.deepcopy(PUBLIC_1))
|
public_mock.return_value = ('file.yaml', copy.deepcopy(PUBLIC_1))
|
||||||
setup_handler.return_value = mock.MagicMock()
|
|
||||||
_shell = make_shell()
|
_shell = make_shell()
|
||||||
|
|
||||||
fake_execute(
|
fake_execute(
|
||||||
@ -668,12 +665,9 @@ class TestShellCli(TestShell):
|
|||||||
|
|
||||||
@mock.patch("os_client_config.config.OpenStackConfig._load_vendor_file")
|
@mock.patch("os_client_config.config.OpenStackConfig._load_vendor_file")
|
||||||
@mock.patch("os_client_config.config.OpenStackConfig._load_config_file")
|
@mock.patch("os_client_config.config.OpenStackConfig._load_config_file")
|
||||||
@mock.patch("openstackclient.common.context._setup_handler_for_logging")
|
def test_shell_args_precedence(self, config_mock, vendor_mock):
|
||||||
def test_shell_args_precedence(self, setup_handler, config_mock,
|
|
||||||
vendor_mock):
|
|
||||||
config_mock.return_value = ('file.yaml', copy.deepcopy(CLOUD_2))
|
config_mock.return_value = ('file.yaml', copy.deepcopy(CLOUD_2))
|
||||||
vendor_mock.return_value = ('file.yaml', copy.deepcopy(PUBLIC_1))
|
vendor_mock.return_value = ('file.yaml', copy.deepcopy(PUBLIC_1))
|
||||||
setup_handler.return_value = mock.MagicMock()
|
|
||||||
_shell = make_shell()
|
_shell = make_shell()
|
||||||
|
|
||||||
# Test command option overriding config file value
|
# Test command option overriding config file value
|
||||||
@ -725,12 +719,9 @@ class TestShellCliEnv(TestShell):
|
|||||||
|
|
||||||
@mock.patch("os_client_config.config.OpenStackConfig._load_vendor_file")
|
@mock.patch("os_client_config.config.OpenStackConfig._load_vendor_file")
|
||||||
@mock.patch("os_client_config.config.OpenStackConfig._load_config_file")
|
@mock.patch("os_client_config.config.OpenStackConfig._load_config_file")
|
||||||
@mock.patch("openstackclient.common.context._setup_handler_for_logging")
|
def test_shell_args_precedence_1(self, config_mock, vendor_mock):
|
||||||
def test_shell_args_precedence_1(self, setup_handler, config_mock,
|
|
||||||
vendor_mock):
|
|
||||||
config_mock.return_value = ('file.yaml', copy.deepcopy(CLOUD_2))
|
config_mock.return_value = ('file.yaml', copy.deepcopy(CLOUD_2))
|
||||||
vendor_mock.return_value = ('file.yaml', copy.deepcopy(PUBLIC_1))
|
vendor_mock.return_value = ('file.yaml', copy.deepcopy(PUBLIC_1))
|
||||||
setup_handler.return_value = mock.MagicMock()
|
|
||||||
_shell = make_shell()
|
_shell = make_shell()
|
||||||
|
|
||||||
# Test env var
|
# Test env var
|
||||||
@ -769,12 +760,9 @@ class TestShellCliEnv(TestShell):
|
|||||||
|
|
||||||
@mock.patch("os_client_config.config.OpenStackConfig._load_vendor_file")
|
@mock.patch("os_client_config.config.OpenStackConfig._load_vendor_file")
|
||||||
@mock.patch("os_client_config.config.OpenStackConfig._load_config_file")
|
@mock.patch("os_client_config.config.OpenStackConfig._load_config_file")
|
||||||
@mock.patch("openstackclient.common.context._setup_handler_for_logging")
|
def test_shell_args_precedence_2(self, config_mock, vendor_mock):
|
||||||
def test_shell_args_precedence_2(self, setup_handler, config_mock,
|
|
||||||
vendor_mock):
|
|
||||||
config_mock.return_value = ('file.yaml', copy.deepcopy(CLOUD_2))
|
config_mock.return_value = ('file.yaml', copy.deepcopy(CLOUD_2))
|
||||||
vendor_mock.return_value = ('file.yaml', copy.deepcopy(PUBLIC_1))
|
vendor_mock.return_value = ('file.yaml', copy.deepcopy(PUBLIC_1))
|
||||||
setup_handler.return_value = mock.MagicMock()
|
|
||||||
_shell = make_shell()
|
_shell = make_shell()
|
||||||
|
|
||||||
# Test command option overriding config file value
|
# Test command option overriding config file value
|
||||||
@ -812,77 +800,3 @@ class TestShellCliEnv(TestShell):
|
|||||||
'krikkit',
|
'krikkit',
|
||||||
_shell.cloud.config['region_name'],
|
_shell.cloud.config['region_name'],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class TestShellCliLogging(TestShell):
|
|
||||||
def setUp(self):
|
|
||||||
super(TestShellCliLogging, self).setUp()
|
|
||||||
|
|
||||||
def tearDown(self):
|
|
||||||
super(TestShellCliLogging, self).tearDown()
|
|
||||||
|
|
||||||
@mock.patch("os_client_config.config.OpenStackConfig._load_vendor_file")
|
|
||||||
@mock.patch("os_client_config.config.OpenStackConfig._load_config_file")
|
|
||||||
@mock.patch("openstackclient.common.context._setup_handler_for_logging")
|
|
||||||
def test_shell_args_precedence_1(self, setup_handler, config_mock,
|
|
||||||
vendor_mock):
|
|
||||||
config_mock.return_value = ('file.yaml', copy.deepcopy(CLOUD_2))
|
|
||||||
vendor_mock.return_value = ('file.yaml', copy.deepcopy(PUBLIC_1))
|
|
||||||
setup_handler.return_value = mock.MagicMock()
|
|
||||||
_shell = make_shell()
|
|
||||||
|
|
||||||
# These come from clouds.yaml
|
|
||||||
fake_execute(
|
|
||||||
_shell,
|
|
||||||
"--os-cloud megacloud list user",
|
|
||||||
)
|
|
||||||
self.assertEqual(
|
|
||||||
'megacloud',
|
|
||||||
_shell.cloud.name,
|
|
||||||
)
|
|
||||||
|
|
||||||
self.assertEqual(
|
|
||||||
'/tmp/test_log_file',
|
|
||||||
_shell.cloud.config['log_file'],
|
|
||||||
)
|
|
||||||
self.assertEqual(
|
|
||||||
'debug',
|
|
||||||
_shell.cloud.config['log_level'],
|
|
||||||
)
|
|
||||||
|
|
||||||
@mock.patch("os_client_config.config.OpenStackConfig._load_vendor_file")
|
|
||||||
@mock.patch("os_client_config.config.OpenStackConfig._load_config_file")
|
|
||||||
def test_shell_args_precedence_2(self, config_mock, vendor_mock):
|
|
||||||
config_mock.return_value = ('file.yaml', copy.deepcopy(CLOUD_1))
|
|
||||||
vendor_mock.return_value = ('file.yaml', copy.deepcopy(PUBLIC_1))
|
|
||||||
_shell = make_shell()
|
|
||||||
|
|
||||||
# Test operation_log_file not set
|
|
||||||
fake_execute(
|
|
||||||
_shell,
|
|
||||||
"--os-cloud scc list user",
|
|
||||||
)
|
|
||||||
self.assertEqual(
|
|
||||||
False,
|
|
||||||
_shell.enable_operation_logging,
|
|
||||||
)
|
|
||||||
|
|
||||||
@mock.patch("os_client_config.config.OpenStackConfig._load_vendor_file")
|
|
||||||
@mock.patch("os_client_config.config.OpenStackConfig._load_config_file")
|
|
||||||
@mock.patch("openstackclient.common.context._setup_handler_for_logging")
|
|
||||||
def test_shell_args_precedence_3(self, setup_handler, config_mock,
|
|
||||||
vendor_mock):
|
|
||||||
config_mock.return_value = ('file.yaml', copy.deepcopy(CLOUD_2))
|
|
||||||
vendor_mock.return_value = ('file.yaml', copy.deepcopy(PUBLIC_1))
|
|
||||||
setup_handler.return_value = mock.MagicMock()
|
|
||||||
_shell = make_shell()
|
|
||||||
|
|
||||||
# Test enable_operation_logging set
|
|
||||||
fake_execute(
|
|
||||||
_shell,
|
|
||||||
"--os-cloud megacloud list user",
|
|
||||||
)
|
|
||||||
self.assertEqual(
|
|
||||||
True,
|
|
||||||
_shell.enable_operation_logging,
|
|
||||||
)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user