From 7610c0fb2e0ec76b254ecb11dffa1c5161da61bf Mon Sep 17 00:00:00 2001 From: Maks Skorokhod Date: Mon, 9 Oct 2017 15:50:36 +0300 Subject: [PATCH 1/3] :wrench: log Connection lost only if debug --- sanic/server.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/sanic/server.py b/sanic/server.py index 8f60a864..06c749a5 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -342,8 +342,10 @@ class HttpProtocol(asyncio.Protocol): self.url, type(response)) self.write_error(ServerError('Invalid response type')) except RuntimeError: - logger.error('Connection lost before response written @ %s', - self.request.ip) + if self._debug: + logger.error('Connection lost before response written @ %s', + self.request.ip) + keep_alive = False except Exception as e: self.bail_out( "Writing response failed, connection closed {}".format( @@ -379,8 +381,10 @@ class HttpProtocol(asyncio.Protocol): self.url, type(response)) self.write_error(ServerError('Invalid response type')) except RuntimeError: - logger.error('Connection lost before response written @ %s', - self.request.ip) + if self._debug: + logger.error('Connection lost before response written @ %s', + self.request.ip) + keep_alive = False except Exception as e: self.bail_out( "Writing response failed, connection closed {}".format( @@ -407,8 +411,9 @@ class HttpProtocol(asyncio.Protocol): version = self.request.version if self.request else '1.1' self.transport.write(response.output(version)) except RuntimeError: - logger.error('Connection lost before error written @ %s', - self.request.ip if self.request else 'Unknown') + if self._debug: + logger.error('Connection lost before error written @ %s', + self.request.ip if self.request else 'Unknown') except Exception as e: self.bail_out( "Writing error failed, connection closed {}".format( From 64edf7ad9c539fc05e70fd8b09f1c485a53472aa Mon Sep 17 00:00:00 2001 From: Maks Skorokhod Date: Mon, 9 Oct 2017 15:50:57 +0300 Subject: [PATCH 2/3] :white_check_mark: upd test for connection lost error --- tests/test_logging.py | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/tests/test_logging.py b/tests/test_logging.py index 112c94a0..4580d6b6 100644 --- a/tests/test_logging.py +++ b/tests/test_logging.py @@ -1,11 +1,17 @@ import uuid +import logging + +from io import StringIO from importlib import reload +import pytest +from unittest.mock import Mock + +import sanic from sanic.response import text from sanic.log import LOGGING_CONFIG_DEFAULTS from sanic import Sanic -from io import StringIO -import logging + logging_format = '''module: %(module)s; \ function: %(funcName)s(); \ @@ -71,3 +77,31 @@ def test_logging_pass_customer_logconfig(): for fmt in [h.formatter for h in logging.getLogger('sanic.access').handlers]: assert fmt._fmt == modified_config['formatters']['access']['format'] + + +@pytest.mark.parametrize('debug', (True, False, )) +def test_log_connection_lost(debug, monkeypatch): + """ Should not log Connection lost exception on non debug """ + app = Sanic('connection_lost') + stream = StringIO() + root = logging.getLogger('root') + root.addHandler(logging.StreamHandler(stream)) + monkeypatch.setattr(sanic.server, 'logger', root) + + @app.route('/conn_lost') + async def conn_lost(request): + response = text('Ok') + response.output = Mock(side_effect=RuntimeError) + return response + + with pytest.raises(ValueError): + # catch ValueError: Exception during request + app.test_client.get('/conn_lost', debug=debug) + + log = stream.getvalue() + + if debug: + assert log.startswith( + 'Connection lost before response written @') + else: + 'Connection lost before response written @' not in log From 4d515b05f3d8e7d3f3074b2f85146a34dc4d875d Mon Sep 17 00:00:00 2001 From: Maks Skorokhod Date: Mon, 9 Oct 2017 17:18:04 +0300 Subject: [PATCH 3/3] :white_check_mark: fix missed assertion --- tests/test_logging.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_logging.py b/tests/test_logging.py index 4580d6b6..e95b7ce5 100644 --- a/tests/test_logging.py +++ b/tests/test_logging.py @@ -104,4 +104,4 @@ def test_log_connection_lost(debug, monkeypatch): assert log.startswith( 'Connection lost before response written @') else: - 'Connection lost before response written @' not in log + assert 'Connection lost before response written @' not in log