diff options
author | Jon Grimm <jgrimm@linux.vnet.ibm.com> | 2015-06-16 05:02:44 +0000 |
---|---|---|
committer | Jon Grimm <jgrimm@linux.vnet.ibm.com> | 2015-06-17 18:43:47 +0000 |
commit | bcd8446a8e154d8ebc0f8ae3b8c77b4d470c8043 (patch) | |
tree | 0df836bf13d43c0c14a36a2fed08f8ac7c1fd2a6 | |
parent | 8f68a2d981fee6f514a45747efecd60018fa927a (diff) | |
download | oslotest-bcd8446a8e154d8ebc0f8ae3b8c77b4d470c8043.tar.gz |
Allow ``OS_DEBUG`` environment variable to specify log level.
Preserve original behavior when ``OS_DEBUG`` is ``True``, but if variable
is set to a valid log level, such as 'WARNING' use that level instead to
allow finer grained control over verbosity of log messages with the
FakeLogger.
Change-Id: I6594cec8de6fa221fe81f663a9126b2df8762ef3
Closes-Bug: 1280454
-rw-r--r-- | oslotest/base.py | 3 | ||||
-rw-r--r-- | oslotest/log.py | 18 | ||||
-rw-r--r-- | tests/unit/test_log.py | 13 |
3 files changed, 29 insertions, 5 deletions
diff --git a/oslotest/base.py b/oslotest/base.py index b77f846..cdea425 100644 --- a/oslotest/base.py +++ b/oslotest/base.py @@ -45,7 +45,8 @@ class BaseTestCase(testtools.TestCase): it produces. If the environment variable ``OS_DEBUG`` is set to a true value, - debug logging is enabled. + debug logging is enabled. Alternatively, the ``OS_DEBUG`` + environment variable can be set to a valid log level. If the environment variable ``OS_LOG_CAPTURE`` is set to a true value, a logging fixture is installed to capture the log output. diff --git a/oslotest/log.py b/oslotest/log.py index 9435e18..94627d9 100644 --- a/oslotest/log.py +++ b/oslotest/log.py @@ -16,6 +16,8 @@ import os import fixtures _TRUE_VALUES = ('True', 'true', '1', 'yes') +_FALSE_VALUES = ('False', 'false', '0', 'no') +_LOG_LEVELS = ('DEBUG', 'INFO', 'WARN', 'WARNING', 'ERROR', 'CRITICAL') class ConfigureLogging(fixtures.Fixture): @@ -23,9 +25,12 @@ class ConfigureLogging(fixtures.Fixture): The behavior is managed through two environment variables. If ``OS_DEBUG`` is true then the logging level is set to debug. If - ``OS_LOG_CAPTURE`` is true a FakeLogger is configured. + ``OS_LOG_CAPTURE`` is true a FakeLogger is configured. Alternatively, + ``OS_DEBUG`` can be set to an explicit log level, such as ``INFO``. - "True" values include ``True``, ``true``, ``1``, and ``yes``. + "True" values include ``True``, ``true``, ``1`` and ``yes``. + Valid log levels include ``DEBUG``, ``INFO``, ``WARNING``, ``ERROR`` + and ``CRITICAL``. .. py:attribute:: logger @@ -34,7 +39,7 @@ class ConfigureLogging(fixtures.Fixture): .. py:attribute:: level ``logging.DEBUG`` if debug logging is enabled, otherwise - ``None``. + the log level specified by ``OS_DEBUG``, otherwise ``None``. :param format: The logging format string to use. @@ -47,8 +52,13 @@ class ConfigureLogging(fixtures.Fixture): super(ConfigureLogging, self).__init__() self._format = format self.level = None - if os.environ.get('OS_DEBUG') in _TRUE_VALUES: + _os_debug = os.environ.get('OS_DEBUG') + if _os_debug in _TRUE_VALUES: self.level = logging.DEBUG + elif _os_debug in _LOG_LEVELS: + self.level = getattr(logging, _os_debug) + elif _os_debug and _os_debug not in _FALSE_VALUES: + raise ValueError('OS_DEBUG=%s is invalid.' % (_os_debug)) self.capture_logs = os.environ.get('OS_LOG_CAPTURE') in _TRUE_VALUES self.logger = None diff --git a/tests/unit/test_log.py b/tests/unit/test_log.py index e87cd0e..6a00337 100644 --- a/tests/unit/test_log.py +++ b/tests/unit/test_log.py @@ -50,6 +50,19 @@ class ConfigureLoggingTestCase(testtools.TestCase): level=logging.DEBUG) @mock.patch('os.environ.get') + @mock.patch('logging.basicConfig') + def test_fake_logs_with_warning(self, basic_logger_mock, env_get_mock): + env_get_mock.side_effect = lambda value, default=None: { + 'OS_DEBUG': 'WARNING', 'OS_LOG_CAPTURE': 0}.get(value, default) + f = log.ConfigureLogging() + f.setUp() + env_get_mock.assert_any_call('OS_LOG_CAPTURE') + env_get_mock.assert_any_calls('OS_DEBUG') + basic_logger_mock.assert_called_once_with( + format=log.ConfigureLogging.DEFAULT_FORMAT, + level=logging.WARNING) + + @mock.patch('os.environ.get') def test_fake_logs_with_log_capture(self, env_get_mock): env_get_mock.side_effect = lambda value: {'OS_DEBUG': 0, 'OS_LOG_CAPTURE': 'True' |