From 8578bdd6d96ab9f732ea8c2144b8504cd406e550 Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Mon, 11 May 2015 14:28:42 -0700 Subject: [PATCH] Allow integer logging levels Integer's are valid level names, and for some libraries they have a lower level than DEBUG that is typically defined at level 5, so to make that accessible, try to convert the level name to a integer, and if not keep the original... Change-Id: I5e7593735112d3e623231c428f680d53a52776a9 --- oslo_log/log.py | 19 ++++++++++++++++--- oslo_log/tests/unit/test_log.py | 4 ++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/oslo_log/log.py b/oslo_log/log.py index d99887b9..d4e240b4 100644 --- a/oslo_log/log.py +++ b/oslo_log/log.py @@ -337,13 +337,26 @@ def _setup_logging_from_conf(conf, project, version): for pair in conf.default_log_levels: mod, _sep, level_name = pair.partition('=') logger = logging.getLogger(mod) + numeric_level = None + try: + # NOTE(harlowja): integer's are valid level names, and for some + # libraries they have a lower level than DEBUG that is typically + # defined at level 5, so to make that accessible, try to convert + # this to a integer, and if not keep the original... + numeric_level = int(level_name) + except ValueError: + pass # NOTE(AAzza) in python2.6 Logger.setLevel doesn't convert string name # to integer code. if sys.version_info < (2, 7): - level = logging.getLevelName(level_name) - logger.setLevel(level) + if numeric_level is None: + numeric_level = logging.getLevelName(level_name) + logger.setLevel(numeric_level) else: - logger.setLevel(level_name) + if numeric_level is not None: + logger.setLevel(numeric_level) + else: + logger.setLevel(level_name) _loggers = {} diff --git a/oslo_log/tests/unit/test_log.py b/oslo_log/tests/unit/test_log.py index 4d15f342..0811c9da 100644 --- a/oslo_log/tests/unit/test_log.py +++ b/oslo_log/tests/unit/test_log.py @@ -216,15 +216,19 @@ def setUp(self): levels = self.CONF.default_log_levels levels.append("nova-test=INFO") levels.append("nova-not-debug=WARN") + levels.append("nova-below-debug=5") self.config(default_log_levels=levels, verbose=True) log.setup(self.CONF, 'testing') self.log = log.getLogger('nova-test') self.log_no_debug = log.getLogger('nova-not-debug') + self.log_below_debug = log.getLogger('nova-below-debug') def test_is_enabled_for(self): self.assertTrue(self.log.isEnabledFor(logging.INFO)) self.assertFalse(self.log_no_debug.isEnabledFor(logging.DEBUG)) + self.assertTrue(self.log_below_debug.isEnabledFor(logging.DEBUG)) + self.assertTrue(self.log_below_debug.isEnabledFor(5)) def test_has_level_from_flags(self): self.assertEqual(logging.INFO, self.log.logger.getEffectiveLevel())