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
This commit is contained in:
parent
8f68a2d981
commit
bcd8446a8e
@ -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.
|
||||
|
@ -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
|
||||
|
||||
|
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user