Allow TRACE and integer logging levels for 'OS_DEBUG'

Change-Id: Id977e71dc64d0e13b2c1077e1dd17c0a38d7354d
This commit is contained in:
Joshua Harlow 2015-07-27 17:50:19 -07:00
parent 294e392c37
commit 358f5b3e03
2 changed files with 45 additions and 4 deletions

View File

@ -17,7 +17,19 @@ import fixtures
_TRUE_VALUES = ('True', 'true', '1', 'yes')
_FALSE_VALUES = ('False', 'false', '0', 'no')
_LOG_LEVELS = ('DEBUG', 'INFO', 'WARN', 'WARNING', 'ERROR', 'CRITICAL')
_BASE_LOG_LEVELS = ('DEBUG', 'INFO', 'WARN', 'WARNING', 'ERROR', 'CRITICAL')
_LOG_LEVELS = dict((n, getattr(logging, n)) for n in _BASE_LOG_LEVELS)
_LOG_LEVELS.update({
'TRACE': 5,
})
def _try_int(value):
"""Try to make some value into an int."""
try:
return int(value)
except (ValueError, TypeError):
return None
class ConfigureLogging(fixtures.Fixture):
@ -29,8 +41,8 @@ class ConfigureLogging(fixtures.Fixture):
``OS_DEBUG`` can be set to an explicit log level, such as ``INFO``.
"True" values include ``True``, ``true``, ``1`` and ``yes``.
Valid log levels include ``DEBUG``, ``INFO``, ``WARNING``, ``ERROR``
and ``CRITICAL``.
Valid log levels include ``DEBUG``, ``INFO``, ``WARNING``, ``ERROR``,
``TRACE`` and ``CRITICAL`` (or any other valid integer logging level).
.. py:attribute:: logger
@ -53,10 +65,13 @@ class ConfigureLogging(fixtures.Fixture):
self._format = format
self.level = None
_os_debug = os.environ.get('OS_DEBUG')
_os_level = _try_int(_os_debug)
if _os_debug in _TRUE_VALUES:
self.level = logging.DEBUG
elif _os_level is not None:
self.level = _os_level
elif _os_debug in _LOG_LEVELS:
self.level = getattr(logging, _os_debug)
self.level = _LOG_LEVELS[_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

View File

@ -62,6 +62,32 @@ class ConfigureLoggingTestCase(testtools.TestCase):
format=log.ConfigureLogging.DEFAULT_FORMAT,
level=logging.WARNING)
@mock.patch('os.environ.get')
@mock.patch('logging.basicConfig')
def test_fake_logs_with_trace_int(self, basic_logger_mock, env_get_mock):
env_get_mock.side_effect = lambda value, default=None: {
'OS_DEBUG': '5', '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_call('OS_DEBUG')
basic_logger_mock.assert_called_once_with(
format=log.ConfigureLogging.DEFAULT_FORMAT,
level=5)
@mock.patch('os.environ.get')
@mock.patch('logging.basicConfig')
def test_fake_logs_with_debug_int(self, basic_logger_mock, env_get_mock):
env_get_mock.side_effect = lambda value, default=None: {
'OS_DEBUG': '10', '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_call('OS_DEBUG')
basic_logger_mock.assert_called_once_with(
format=log.ConfigureLogging.DEFAULT_FORMAT,
level=logging.DEBUG)
@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,