From b63c06c75a54752d7f3115d3c635580db44b8399 Mon Sep 17 00:00:00 2001 From: Meng Wang Date: Tue, 6 Nov 2018 22:39:38 +0800 Subject: [PATCH] fix the logger and make it work (#1397) * fix the logger and make it work * modify test_logging parameters and add a new unit test --- sanic/log.py | 2 +- tests/test_logging.py | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/sanic/log.py b/sanic/log.py index cb8ca524..08fc835d 100644 --- a/sanic/log.py +++ b/sanic/log.py @@ -6,7 +6,7 @@ LOGGING_CONFIG_DEFAULTS = dict( version=1, disable_existing_loggers=False, loggers={ - "root": {"level": "INFO", "handlers": ["console"]}, + "sanic.root": {"level": "INFO", "handlers": ["console"]}, "sanic.error": { "level": "INFO", "handlers": ["error_console"], diff --git a/tests/test_logging.py b/tests/test_logging.py index 3af3f122..95c55de0 100644 --- a/tests/test_logging.py +++ b/tests/test_logging.py @@ -49,7 +49,7 @@ def test_logging_defaults(): reset_logging() app = Sanic("test_logging") - for fmt in [h.formatter for h in logging.getLogger('root').handlers]: + for fmt in [h.formatter for h in logging.getLogger('sanic.root').handlers]: assert fmt._fmt == LOGGING_CONFIG_DEFAULTS['formatters']['generic']['format'] for fmt in [h.formatter for h in logging.getLogger('sanic.error').handlers]: @@ -68,7 +68,7 @@ def test_logging_pass_customer_logconfig(): app = Sanic("test_logging", log_config=modified_config) - for fmt in [h.formatter for h in logging.getLogger('root').handlers]: + for fmt in [h.formatter for h in logging.getLogger('sanic.root').handlers]: assert fmt._fmt == modified_config['formatters']['generic']['format'] for fmt in [h.formatter for h in logging.getLogger('sanic.error').handlers]: @@ -82,7 +82,7 @@ def test_logging_pass_customer_logconfig(): def test_log_connection_lost(app, debug, monkeypatch): """ Should not log Connection lost exception on non debug """ stream = StringIO() - root = logging.getLogger('root') + root = logging.getLogger('sanic.root') root.addHandler(logging.StreamHandler(stream)) monkeypatch.setattr(sanic.server, 'logger', root) @@ -102,3 +102,15 @@ def test_log_connection_lost(app, debug, monkeypatch): assert 'Connection lost before response written @' in log else: assert 'Connection lost before response written @' not in log + + +def test_logging_modified_root_logger_config(): + reset_logging() + + modified_config = LOGGING_CONFIG_DEFAULTS + modified_config['loggers']['sanic.root']['level'] = 'DEBUG' + + app = Sanic("test_logging", log_config=modified_config) + + assert logging.getLogger('sanic.root').getEffectiveLevel() == logging.DEBUG +