summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElena Ezhova <eezhova@mirantis.com>2015-11-27 15:03:01 +0300
committerElena Ezhova <eezhova@mirantis.com>2015-12-01 14:02:41 +0300
commitabbc1701d00b9d6dbe17e54dac31dff6dd1070f9 (patch)
tree6359039fb6c3a7fa5c168846157638e4686f79ad
parentab89e5b4541cd23940e06913eab7e5b5ba243413 (diff)
downloadoslo-log-stable/liberty.tar.gz
Cleanup all handlers in _setup_logging_from_confliberty-eolstable/liberty
_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. Conflicts: oslo_log/tests/unit/test_log.py Change-Id: I869f12a7ca8f6672a344159474d8b3d4ee584e57 Closes-Bug: #1520235 (cherry picked from commit fcf3115539d191e3ad166b12a08c4c662ebf5c17)
-rw-r--r--oslo_log/log.py4
-rw-r--r--oslo_log/tests/unit/test_log.py11
2 files changed, 14 insertions, 1 deletions
diff --git a/oslo_log/log.py b/oslo_log/log.py
index 3ad071f..d97ff5d 100644
--- a/oslo_log/log.py
+++ b/oslo_log/log.py
@@ -306,7 +306,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)
diff --git a/oslo_log/tests/unit/test_log.py b/oslo_log/tests/unit/test_log.py
index 5159dec..461a69e 100644
--- a/oslo_log/tests/unit/test_log.py
+++ b/oslo_log/tests/unit/test_log.py
@@ -117,6 +117,7 @@ class BaseTestCase(test_base.BaseTestCase):
self.config = self.config_fixture.config
self.CONF = self.config_fixture.conf
log.register_options(self.CONF)
+ log.setup(self.CONF, 'base')
class LogTestBase(BaseTestCase):
@@ -758,6 +759,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):