Cleanup all handlers in _setup_logging_from_conf

_setup_logging_from_conf doesn't remove all previously registered
handlers and some of them may get registered twice which can
result in duplicating logs.

Iterating through a collection and removing items from it is bad
practice and should be avoided.

Change-Id: I869f12a7ca8f6672a344159474d8b3d4ee584e57
Closes-Bug: #1520235
This commit is contained in:
Elena Ezhova 2015-11-27 15:03:01 +03:00
parent c9575f4d87
commit fcf3115539
2 changed files with 13 additions and 1 deletions

View File

@ -303,7 +303,9 @@ def _find_facility(facility):
def _setup_logging_from_conf(conf, project, version):
log_root = getLogger(None).logger
for handler in log_root.handlers:
# Remove all handlers
for handler in list(log_root.handlers):
log_root.removeHandler(handler)
logpath = _get_log_file_path(conf)

View File

@ -845,6 +845,16 @@ class LogConfigOptsTestCase(BaseTestCase):
self.assertTrue(isinstance(formatter,
formatters.ContextFormatter))
def test_handlers_cleanup(self):
"""Test that all old handlers get removed from log_root."""
old_handlers = [log.handlers.ColorHandler(),
log.handlers.ColorHandler()]
log._loggers[None].logger.handlers = list(old_handlers)
log._setup_logging_from_conf(self.CONF, 'test', 'test')
handlers = log._loggers[None].logger.handlers
self.assertEqual(1, len(handlers))
self.assertNotIn(handlers[0], old_handlers)
class LogConfigTestCase(BaseTestCase):