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 @@ -49,6 +49,19 @@ class ConfigureLoggingTestCase(testtools.TestCase): format=log.ConfigureLogging.DEFAULT_FORMAT, 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,