allow keeping of existing loggers with fileConfig
Currently, common.log.py setup() will disable all existing loggers that were set up before importing a custom logging configuration from file. This patch deprecates option name 'log-config' in favor of 'log-config-append' and makes disable_existing_loggers with default value False. Closes-Bug: #1169328 Closes-Bug: #1238349 DocImpact Change-Id: I1f25300d44b04ca5bcdd9b505319f0d089a9c964
This commit is contained in:
@@ -64,11 +64,13 @@ common_cli_opts = [
|
||||
]
|
||||
|
||||
logging_cli_opts = [
|
||||
cfg.StrOpt('log-config',
|
||||
cfg.StrOpt('log-config-append',
|
||||
metavar='PATH',
|
||||
help='If this option is specified, the logging configuration '
|
||||
'file specified is used and overrides any other logging '
|
||||
'options specified. Please see the Python logging module '
|
||||
deprecated_name='log-config',
|
||||
help='The name of logging configuration file. It does not '
|
||||
'disable existing loggers, but just appends specified '
|
||||
'logging configuration to any other existing logging '
|
||||
'options. Please see the Python logging module '
|
||||
'documentation for details on logging configuration '
|
||||
'files.'),
|
||||
cfg.StrOpt('log-format',
|
||||
@@ -355,17 +357,18 @@ class LogConfigError(Exception):
|
||||
err_msg=self.err_msg)
|
||||
|
||||
|
||||
def _load_log_config(log_config):
|
||||
def _load_log_config(log_config_append):
|
||||
try:
|
||||
logging.config.fileConfig(log_config)
|
||||
logging.config.fileConfig(log_config_append,
|
||||
disable_existing_loggers=False)
|
||||
except moves.configparser.Error as exc:
|
||||
raise LogConfigError(log_config, str(exc))
|
||||
raise LogConfigError(log_config_append, str(exc))
|
||||
|
||||
|
||||
def setup(product_name):
|
||||
"""Setup logging."""
|
||||
if CONF.log_config:
|
||||
_load_log_config(CONF.log_config)
|
||||
if CONF.log_config_append:
|
||||
_load_log_config(CONF.log_config_append)
|
||||
else:
|
||||
_setup_logging_from_conf()
|
||||
sys.excepthook = _create_logging_excepthook(product_name)
|
||||
|
@@ -19,6 +19,7 @@ import os
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
import mock
|
||||
from oslo.config import cfg
|
||||
import six
|
||||
|
||||
@@ -421,7 +422,7 @@ class LogConfigOptsTestCase(test.BaseTestCase):
|
||||
def test_logging_opts(self):
|
||||
self.CONF([])
|
||||
|
||||
self.assertTrue(self.CONF.log_config is None)
|
||||
self.assertTrue(self.CONF.log_config_append is None)
|
||||
self.assertTrue(self.CONF.log_file is None)
|
||||
self.assertTrue(self.CONF.log_dir is None)
|
||||
self.assertTrue(self.CONF.log_format is None)
|
||||
@@ -494,24 +495,41 @@ handlers=
|
||||
os.close(fd)
|
||||
return path
|
||||
|
||||
def test_log_config_ok(self):
|
||||
log_config = self._create_tempfile('logging', self.minimal_config)
|
||||
self.config(log_config=log_config)
|
||||
log.setup('test_log_config')
|
||||
def test_log_config_append_ok(self):
|
||||
log_config_append = self._create_tempfile('logging',
|
||||
self.minimal_config)
|
||||
self.config(log_config_append=log_config_append)
|
||||
log.setup('test_log_config_append')
|
||||
|
||||
def test_log_config_not_exist(self):
|
||||
log_config = self._create_tempfile('logging', self.minimal_config)
|
||||
os.remove(log_config)
|
||||
self.config(log_config=log_config)
|
||||
self.assertRaises(log.LogConfigError, log.setup, 'test_log_config')
|
||||
def test_log_config_append_not_exist(self):
|
||||
log_config_append = self._create_tempfile('logging',
|
||||
self.minimal_config)
|
||||
os.remove(log_config_append)
|
||||
self.config(log_config_append=log_config_append)
|
||||
self.assertRaises(log.LogConfigError, log.setup,
|
||||
'test_log_config_append')
|
||||
|
||||
def test_log_config_invalid(self):
|
||||
log_config = self._create_tempfile('logging', self.minimal_config[5:])
|
||||
self.config(log_config=log_config)
|
||||
self.assertRaises(log.LogConfigError, log.setup, 'test_log_config')
|
||||
def test_log_config_append_invalid(self):
|
||||
log_config_append = self._create_tempfile('logging',
|
||||
self.minimal_config[5:])
|
||||
self.config(log_config_append=log_config_append)
|
||||
self.assertRaises(log.LogConfigError, log.setup,
|
||||
'test_log_config_append')
|
||||
|
||||
def test_log_config_unreadable(self):
|
||||
log_config = self._create_tempfile('logging', self.minimal_config)
|
||||
os.chmod(log_config, 0)
|
||||
self.config(log_config=log_config)
|
||||
self.assertRaises(log.LogConfigError, log.setup, 'test_log_config')
|
||||
def test_log_config_append_unreadable(self):
|
||||
log_config_append = self._create_tempfile('logging',
|
||||
self.minimal_config)
|
||||
os.chmod(log_config_append, 0)
|
||||
self.config(log_config_append=log_config_append)
|
||||
self.assertRaises(log.LogConfigError, log.setup,
|
||||
'test_log_config_append')
|
||||
|
||||
def test_log_config_append_disable_existing_loggers(self):
|
||||
log_config_append = self._create_tempfile('logging',
|
||||
self.minimal_config)
|
||||
self.config(log_config_append=log_config_append)
|
||||
with mock.patch('logging.config.fileConfig') as fileConfig:
|
||||
log.setup('test_log_config_append')
|
||||
|
||||
fileConfig.assert_called_once_with(log_config_append,
|
||||
disable_existing_loggers=False)
|
||||
|
Reference in New Issue
Block a user