From ba1e00658578b27316d676dcaeedd1acb9bd23b6 Mon Sep 17 00:00:00 2001 From: Raphael Deem Date: Tue, 17 Jan 2017 15:38:20 -0800 Subject: [PATCH] update logging placement --- examples/override_logging.py | 2 +- sanic/sanic.py | 22 ++++++++++++++-------- tests/test_logging.py | 2 +- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/examples/override_logging.py b/examples/override_logging.py index 25fd78de..117d63bf 100644 --- a/examples/override_logging.py +++ b/examples/override_logging.py @@ -14,7 +14,7 @@ logging.basicConfig( log = logging.getLogger() # Set logger to override default basicConfig -sanic = Sanic(logger=True) +sanic = Sanic() @sanic.route("/") def test(request): log.info("received request; responding with 'hey'") diff --git a/sanic/sanic.py b/sanic/sanic.py index 6926050c..5f8aeeb2 100644 --- a/sanic/sanic.py +++ b/sanic/sanic.py @@ -21,12 +21,7 @@ from os import set_inheritable class Sanic: def __init__(self, name=None, router=None, - error_handler=None, logger=None): - if logger is None: - logging.basicConfig( - level=logging.INFO, - format="%(asctime)s: %(levelname)s: %(message)s" - ) + error_handler=None): if name is None: frame_records = stack()[1] name = getmodulename(frame_records[1]) @@ -154,7 +149,8 @@ class Sanic: def register_blueprint(self, *args, **kwargs): # TODO: deprecate 1.0 log.warning("Use of register_blueprint will be deprecated in " - "version 1.0. Please use the blueprint method instead") + "version 1.0. Please use the blueprint method instead", + DeprecationWarning) return self.blueprint(*args, **kwargs) # -------------------------------------------------------------------- # @@ -247,7 +243,7 @@ class Sanic: def run(self, host="127.0.0.1", port=8000, debug=False, before_start=None, after_start=None, before_stop=None, after_stop=None, sock=None, workers=1, loop=None, protocol=HttpProtocol, backlog=100, - stop_event=None): + stop_event=None, logger=None): """ Runs the HTTP Server and listens until keyboard interrupt or term signal. On termination, drains connections before closing. @@ -269,6 +265,10 @@ class Sanic: :param protocol: Subclass of asyncio protocol class :return: Nothing """ + logging.basicConfig( + level=logging.INFO, + format="%(asctime)s: %(levelname)s: %(message)s" + ) self.error_handler.debug = True self.debug = debug self.loop = loop @@ -350,6 +350,12 @@ class Sanic: :param stop_event: if provided, is used as a stop signal :return: """ + # In case this is called directly, we configure logging here too. + # This won't interfere with the same call from run() + logging.basicConfig( + level=logging.INFO, + format="%(asctime)s: %(levelname)s: %(message)s" + ) server_settings['reuse_port'] = True # Create a stop event to be triggered by a signal diff --git a/tests/test_logging.py b/tests/test_logging.py index 65de28c2..b3e3c1fc 100644 --- a/tests/test_logging.py +++ b/tests/test_logging.py @@ -19,7 +19,7 @@ def test_log(): stream=log_stream ) log = logging.getLogger() - app = Sanic('test_logging', logger=True) + app = Sanic('test_logging') @app.route('/') def handler(request): log.info('hello world')