Merge "Allow reload of 'debug' option"
This commit is contained in:
commit
440a3c2267
@ -34,6 +34,7 @@ common_cli_opts = [
|
||||
cfg.BoolOpt('debug',
|
||||
short='d',
|
||||
default=False,
|
||||
mutable=True,
|
||||
help='If set to true, the logging level will be set to '
|
||||
'DEBUG instead of the default INFO level.'),
|
||||
cfg.BoolOpt('verbose',
|
||||
|
@ -219,6 +219,15 @@ def _load_log_config(log_config_append):
|
||||
raise LogConfigError(log_config_append, six.text_type(exc))
|
||||
|
||||
|
||||
def _mutate_hook(conf, fresh):
|
||||
"""Reconfigures oslo.log according to the mutated options."""
|
||||
|
||||
# verbose is deprecated, so I didn't make it mutable, so there's no need to
|
||||
# test for it here.
|
||||
if (None, 'debug') in fresh:
|
||||
_refresh_root_level(conf.debug, conf.verbose)
|
||||
|
||||
|
||||
def register_options(conf):
|
||||
"""Register the command line and configuration options used by oslo.log."""
|
||||
|
||||
@ -234,6 +243,8 @@ def register_options(conf):
|
||||
conf.register_opts(_options.log_opts)
|
||||
formatters._store_global_conf(conf)
|
||||
|
||||
conf.register_mutate_hook(_mutate_hook)
|
||||
|
||||
|
||||
def setup(conf, product_name, version='unknown'):
|
||||
"""Setup logging for the current application."""
|
||||
@ -301,6 +312,27 @@ def _find_facility(facility):
|
||||
return getattr(syslog, facility)
|
||||
|
||||
|
||||
def _refresh_root_level(debug, verbose):
|
||||
"""Set the level of the root logger.
|
||||
|
||||
If 'debug' is True, the level will be DEBUG. Otherwise we look at 'verbose'
|
||||
- if that is True, the level will be INFO. If neither are set, the level
|
||||
will be WARNING.
|
||||
|
||||
Note the 'verbose' option is deprecated.
|
||||
|
||||
:param debug
|
||||
:param verbose
|
||||
"""
|
||||
log_root = getLogger(None).logger
|
||||
if debug:
|
||||
log_root.setLevel(logging.DEBUG)
|
||||
elif verbose:
|
||||
log_root.setLevel(logging.INFO)
|
||||
else:
|
||||
log_root.setLevel(logging.WARNING)
|
||||
|
||||
|
||||
def _setup_logging_from_conf(conf, project, version):
|
||||
log_root = getLogger(None).logger
|
||||
|
||||
@ -349,13 +381,7 @@ def _setup_logging_from_conf(conf, project, version):
|
||||
version=version,
|
||||
datefmt=datefmt,
|
||||
config=conf))
|
||||
|
||||
if conf.debug:
|
||||
log_root.setLevel(logging.DEBUG)
|
||||
elif conf.verbose:
|
||||
log_root.setLevel(logging.INFO)
|
||||
else:
|
||||
log_root.setLevel(logging.WARNING)
|
||||
_refresh_root_level(conf.debug, conf.verbose)
|
||||
|
||||
for pair in conf.default_log_levels:
|
||||
mod, _sep, level_name = pair.partition('=')
|
||||
|
@ -19,6 +19,7 @@ import datetime
|
||||
import logging
|
||||
import os
|
||||
import platform
|
||||
import shutil
|
||||
import sys
|
||||
try:
|
||||
import syslog
|
||||
@ -857,6 +858,32 @@ class FastWatchedFileHandlerTestCase(BaseTestCase):
|
||||
self.assertTrue(os.path.exists(log_path))
|
||||
|
||||
|
||||
class ConfigTestCase(test_base.BaseTestCase):
|
||||
def test_mutate(self):
|
||||
conf = cfg.CONF
|
||||
old_config = ("[DEFAULT]\n"
|
||||
"debug = false\n")
|
||||
new_config = ("[DEFAULT]\n"
|
||||
"debug = true\n")
|
||||
paths = self.create_tempfiles([('old', old_config),
|
||||
('new', new_config)])
|
||||
log.register_options(conf)
|
||||
conf(['--config-file', paths[0]])
|
||||
log_root = log.getLogger(None).logger
|
||||
|
||||
log._setup_logging_from_conf(conf, 'test', 'test')
|
||||
self.assertEqual(conf.debug, False)
|
||||
self.assertEqual(conf.verbose, True)
|
||||
self.assertEqual(log.INFO, log_root.getEffectiveLevel())
|
||||
|
||||
shutil.copy(paths[1], paths[0])
|
||||
conf.mutate_config_files()
|
||||
|
||||
self.assertEqual(conf.debug, True)
|
||||
self.assertEqual(conf.verbose, True)
|
||||
self.assertEqual(log.DEBUG, log_root.getEffectiveLevel())
|
||||
|
||||
|
||||
class LogConfigOptsTestCase(BaseTestCase):
|
||||
|
||||
def setUp(self):
|
||||
|
Loading…
x
Reference in New Issue
Block a user