From e6ef43c0506e08aa5e5b475e11dbf8ff9f0f3773 Mon Sep 17 00:00:00 2001 From: Dave Shawley Date: Tue, 3 Sep 2019 07:29:20 -0400 Subject: [PATCH] Deprecate calling run without a logging config. --- docs/history.rst | 2 ++ examples.py | 28 ++++++++++++++++++++++++++-- sprockets/http/__init__.py | 17 ++++++++++++++--- tests.py | 9 +++++++++ 4 files changed, 51 insertions(+), 5 deletions(-) diff --git a/docs/history.rst b/docs/history.rst index 24b9133..728e574 100644 --- a/docs/history.rst +++ b/docs/history.rst @@ -7,6 +7,8 @@ Release History --------------- - Make shutdown timings configurable. - Add :class:`sprockets.http.testing.SprocketsHttpTestCase`. +- Deprecate calling :func:`sprockets.http.run` without a specified + logging configuration. `2.0.1`_ (5 Mar 2019) ---------------------- diff --git a/examples.py b/examples.py index 306c288..08e5d02 100644 --- a/examples.py +++ b/examples.py @@ -1,6 +1,7 @@ from tornado import web -from sprockets.http import app, mixins, run +from sprockets.http import app, mixins +import sprockets.http class StatusHandler(mixins.ErrorLogger, mixins.ErrorWriter, @@ -37,4 +38,27 @@ class Application(app.Application): if __name__ == '__main__': - run(Application) + sprockets.http.run( + Application, + settings={'port': 8888}, + log_config={ + 'version': 1, + 'disable_existing_loggers': False, + 'formatters': { + 'readable': { + 'format': '%(levelname)-13s %(name)s: %(message)s', + } + }, + 'handlers': { + 'console': { + 'class': 'logging.StreamHandler', + 'formatter': 'readable', + 'stream': 'ext://sys.stdout', + } + }, + 'root': { + 'level': 'DEBUG', + 'handlers': ['console'], + } + }, + ) diff --git a/sprockets/http/__init__.py b/sprockets/http/__init__.py index 4aa38ec..e015c26 100644 --- a/sprockets/http/__init__.py +++ b/sprockets/http/__init__.py @@ -1,13 +1,16 @@ import logging import logging.config import os +import warnings version_info = (2, 0, 1) __version__ = '.'.join(str(v) for v in version_info) +_unspecified = object() -def run(create_application, settings=None, log_config=None): + +def run(create_application, settings=None, log_config=_unspecified): """ Run a Tornado create_application. @@ -61,8 +64,16 @@ def run(create_application, settings=None, log_config=None): debug_mode = bool(app_settings.get('debug', int(os.environ.get('DEBUG', 0)) != 0)) app_settings['debug'] = debug_mode - logging.config.dictConfig(_get_logging_config(debug_mode) - if log_config is None else log_config) + if log_config is _unspecified: + warnings.warn( + 'calling sprockets.http.run without logging configuration is ' + 'deprecated and will fail in the future', DeprecationWarning) + logging.config.dictConfig(_get_logging_config(debug_mode)) + logging.warning( + 'calling sprockets.http.run without logging configuration is ' + 'deprecated and will fail in the future') + else: + logging.config.dictConfig(log_config) 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/tests.py b/tests.py index afb5a05..5ea5954 100644 --- a/tests.py +++ b/tests.py @@ -9,6 +9,7 @@ import json import time import unittest import uuid +import warnings from tornado import concurrent, httpserver, httputil, ioloop, testing, web @@ -296,6 +297,14 @@ class RunTests(MockHelper, unittest.TestCase): self.logging_dict_config.assert_called_once_with( mock.sentinel.config) + def test_that_not_specifying_logging_config_is_deprecated(self): + with warnings.catch_warnings(record=True) as captured: + warnings.simplefilter('always') + sprockets.http.run(mock.Mock()) + + self.assertEqual(len(captured), 1) + self.assertTrue(issubclass(captured[0].category, DeprecationWarning)) + class CallbackTests(MockHelper, unittest.TestCase):