From 557989399cc79cbccc03a597448422d0941f77a1 Mon Sep 17 00:00:00 2001 From: Dave Shawley Date: Thu, 10 Dec 2015 10:41:59 -0500 Subject: [PATCH] Move logging.config.dictConfig into http.run. --- sprockets/http/__init__.py | 3 ++- sprockets/http/runner.py | 10 ++++------ tests.py | 24 ++++++++++++++++-------- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/sprockets/http/__init__.py b/sprockets/http/__init__.py index 290be4f..d27638c 100644 --- a/sprockets/http/__init__.py +++ b/sprockets/http/__init__.py @@ -1,3 +1,4 @@ +import logging import os @@ -61,7 +62,7 @@ def run(create_application, settings=None): debug_mode = bool(app_settings.get('debug', int(os.environ.get('DEBUG', 0)) != 0)) app_settings['debug'] = debug_mode - runner._configure_logging(debug_mode) + logging.config.dictConfig(runner._get_logging_config(debug_mode)) port_number = int(app_settings.pop('port', os.environ.get('PORT', 8000))) num_procs = int(app_settings.pop('number_of_procs', '0')) diff --git a/sprockets/http/runner.py b/sprockets/http/runner.py index 30ccb75..e9e8213 100644 --- a/sprockets/http/runner.py +++ b/sprockets/http/runner.py @@ -120,15 +120,15 @@ class Runner(object): maybe_stop() -def _configure_logging(debug): +def _get_logging_config(debug): """ - Configure the ``logging`` package appropriately. + Retrieve dictionary-based logging configuration. :param bool debug: are we running in debug mode? """ if debug: - log_config = { + return { 'version': 1, 'disable_existing_loggers': False, 'incremental': False, @@ -152,7 +152,7 @@ def _configure_logging(debug): } } else: - log_config = { + return { 'version': 1, 'disable_existing_loggers': False, 'incremental': False, @@ -172,5 +172,3 @@ def _configure_logging(debug): 'handlers': ['console'], } } - - logging.config.dictConfig(log_config) diff --git a/tests.py b/tests.py index 468bdc8..87fe98d 100644 --- a/tests.py +++ b/tests.py @@ -216,8 +216,11 @@ class RunTests(MockHelper, unittest.TestCase): def setUp(self): super(RunTests, self).setUp() self.runner_cls = self.start_mock('sprockets.http.runner.Runner') - self.configure_logging = self.start_mock( - 'sprockets.http.runner._configure_logging') + self.get_logging_config = self.start_mock( + 'sprockets.http.runner._get_logging_config') + self.get_logging_config.return_value = {'version': 1} + self.logging_dict_config = self.start_mock( + 'sprockets.http.logging.config').dictConfig @property def runner_instance(self): @@ -234,21 +237,21 @@ class RunTests(MockHelper, unittest.TestCase): with override_environment_variable('DEBUG', '1'): sprockets.http.run(create_app) create_app.assert_called_once_with(debug=True) - self.configure_logging.assert_called_once_with(True) + self.get_logging_config.assert_called_once_with(True) def test_that_false_debug_envvar_disables_debug_flag(self): create_app = mock.Mock() with override_environment_variable('DEBUG', '0'): sprockets.http.run(create_app) create_app.assert_called_once_with(debug=False) - self.configure_logging.assert_called_once_with(False) + self.get_logging_config.assert_called_once_with(False) def test_that_unset_debug_envvar_disables_debug_flag(self): create_app = mock.Mock() with override_environment_variable('DEBUG', None): sprockets.http.run(create_app) create_app.assert_called_once_with(debug=False) - self.configure_logging.assert_called_once_with(False) + self.get_logging_config.assert_called_once_with(False) def test_that_port_defaults_to_8000(self): sprockets.http.run(mock.Mock()) @@ -261,12 +264,17 @@ class RunTests(MockHelper, unittest.TestCase): def test_that_port_kwarg_sets_port_number(self): sprockets.http.run(mock.Mock(), settings={'port': 8888}) - self.runner_instance.run.called_once_with(8888, mock.ANY) + self.runner_instance.run.assert_called_once_with(8888, mock.ANY) def test_that_number_of_procs_defaults_to_zero(self): sprockets.http.run(mock.Mock()) - self.runner_instance.run.called_once_with(mock.ANY, 0) + self.runner_instance.run.assert_called_once_with(mock.ANY, 0) def test_that_number_of_process_kwarg_sets_number_of_procs(self): sprockets.http.run(mock.Mock(), settings={'number_of_procs': 1}) - self.runner_instance.run.called_once_with(mock.ANY, 1) + self.runner_instance.run.assert_called_once_with(mock.ANY, 1) + + def test_that_logging_dict_config_is_called_appropriately(self): + sprockets.http.run(mock.Mock()) + self.logging_dict_config.assert_called_once_with( + self.get_logging_config.return_value)