From 42efb8cbb482d42642eccd452d848d48531ac6d2 Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Thu, 17 Jul 2014 12:50:10 -0700 Subject: [PATCH] Adjust oslo logging to provide adapter is enabled for In python 2.6 the isEnabledFor adapter method is not found since it was added in 2.7+ so to enable its usage in openstack which can want to use this method add it on when in python 2.6 and just call the parent method on other versions. Closes-Bug: #1343544 Change-Id: I5c3ba91cb1a8555d649fffc1377e9e9dfe92fea8 --- openstack/common/log.py | 11 +++++++++++ tests/unit/test_log.py | 6 ++++++ 2 files changed, 17 insertions(+) diff --git a/openstack/common/log.py b/openstack/common/log.py index 7c539931..39c182e7 100644 --- a/openstack/common/log.py +++ b/openstack/common/log.py @@ -40,6 +40,8 @@ from oslo.config import cfg import six from six import moves +_PY26 = sys.version_info[0:2] == (2, 6) + from openstack.common.gettextutils import _ from openstack.common import importutils from openstack.common import jsonutils @@ -231,6 +233,15 @@ class BaseLoggerAdapter(logging.LoggerAdapter): def audit(self, msg, *args, **kwargs): self.log(logging.AUDIT, msg, *args, **kwargs) + def isEnabledFor(self, level): + if _PY26: + # This method was added in python 2.7 (and it does the exact + # same logic, so we need to do the exact same logic so that + # python 2.6 has this capability as well). + return self.logger.isEnabledFor(level) + else: + return super(BaseLoggerAdapter, self).isEnabledFor(level) + class LazyAdapter(BaseLoggerAdapter): def __init__(self, name='unknown', version='unknown'): diff --git a/tests/unit/test_log.py b/tests/unit/test_log.py index 5651aa58..f5a3699c 100644 --- a/tests/unit/test_log.py +++ b/tests/unit/test_log.py @@ -258,11 +258,17 @@ class LogLevelTestCase(test_base.BaseTestCase): self.CONF = self.useFixture(config.Config()).conf levels = self.CONF.default_log_levels levels.append("nova-test=AUDIT") + levels.append("nova-not-debug=WARN") self.config = self.useFixture(config.Config()).config self.config(default_log_levels=levels, verbose=True) log.setup('testing') self.log = log.getLogger('nova-test') + self.log_no_debug = log.getLogger('nova-not-debug') + + def test_is_enabled_for(self): + self.assertTrue(self.log.isEnabledFor(logging.AUDIT)) + self.assertFalse(self.log_no_debug.isEnabledFor(logging.DEBUG)) def test_has_level_from_flags(self): self.assertEqual(logging.AUDIT, self.log.logger.getEffectiveLevel())