Consistent use of error loggers (#2109)
* Consistent use of error loggers * Fix tests
This commit is contained in:
parent
ad97cac313
commit
53a571ec6c
|
@ -8,7 +8,7 @@ from typing import Any, Dict, Optional
|
||||||
from sanic import __version__
|
from sanic import __version__
|
||||||
from sanic.app import Sanic
|
from sanic.app import Sanic
|
||||||
from sanic.config import BASE_LOGO
|
from sanic.config import BASE_LOGO
|
||||||
from sanic.log import logger
|
from sanic.log import error_logger
|
||||||
|
|
||||||
|
|
||||||
class SanicArgumentParser(ArgumentParser):
|
class SanicArgumentParser(ArgumentParser):
|
||||||
|
@ -119,13 +119,13 @@ def main():
|
||||||
ssl=ssl,
|
ssl=ssl,
|
||||||
)
|
)
|
||||||
except ImportError as e:
|
except ImportError as e:
|
||||||
logger.error(
|
error_logger.error(
|
||||||
f"No module named {e.name} found.\n"
|
f"No module named {e.name} found.\n"
|
||||||
f" Example File: project/sanic_server.py -> app\n"
|
f" Example File: project/sanic_server.py -> app\n"
|
||||||
f" Example Module: project.sanic_server.app"
|
f" Example Module: project.sanic_server.app"
|
||||||
)
|
)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
logger.exception("Failed to run app")
|
error_logger.exception("Failed to run app")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
@ -6,7 +6,7 @@ from sanic.exceptions import (
|
||||||
HeaderNotFound,
|
HeaderNotFound,
|
||||||
InvalidRangeType,
|
InvalidRangeType,
|
||||||
)
|
)
|
||||||
from sanic.log import logger
|
from sanic.log import error_logger
|
||||||
from sanic.response import text
|
from sanic.response import text
|
||||||
|
|
||||||
|
|
||||||
|
@ -101,7 +101,7 @@ class ErrorHandler:
|
||||||
response_message = (
|
response_message = (
|
||||||
"Exception raised in exception handler " '"%s" for uri: %s'
|
"Exception raised in exception handler " '"%s" for uri: %s'
|
||||||
)
|
)
|
||||||
logger.exception(response_message, handler.__name__, url)
|
error_logger.exception(response_message, handler.__name__, url)
|
||||||
|
|
||||||
if self.debug:
|
if self.debug:
|
||||||
return text(response_message % (handler.__name__, url), 500)
|
return text(response_message % (handler.__name__, url), 500)
|
||||||
|
@ -137,7 +137,9 @@ class ErrorHandler:
|
||||||
url = "unknown"
|
url = "unknown"
|
||||||
|
|
||||||
self.log(format_exc())
|
self.log(format_exc())
|
||||||
logger.exception("Exception occurred while handling uri: %s", url)
|
error_logger.exception(
|
||||||
|
"Exception occurred while handling uri: %s", url
|
||||||
|
)
|
||||||
|
|
||||||
return exception_response(request, exception, self.debug)
|
return exception_response(request, exception, self.debug)
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ from sanic.exceptions import (
|
||||||
)
|
)
|
||||||
from sanic.headers import format_http1_response
|
from sanic.headers import format_http1_response
|
||||||
from sanic.helpers import has_message_body
|
from sanic.helpers import has_message_body
|
||||||
from sanic.log import access_logger, logger
|
from sanic.log import access_logger, error_logger, logger
|
||||||
|
|
||||||
|
|
||||||
class Stage(Enum):
|
class Stage(Enum):
|
||||||
|
@ -143,7 +143,7 @@ class Http:
|
||||||
# Try to consume any remaining request body
|
# Try to consume any remaining request body
|
||||||
if self.request_body:
|
if self.request_body:
|
||||||
if self.response and 200 <= self.response.status < 300:
|
if self.response and 200 <= self.response.status < 300:
|
||||||
logger.error(f"{self.request} body not consumed.")
|
error_logger.error(f"{self.request} body not consumed.")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async for _ in self:
|
async for _ in self:
|
||||||
|
|
|
@ -39,7 +39,7 @@ from sanic.compat import OS_IS_WINDOWS, ctrlc_workaround_for_windows
|
||||||
from sanic.config import Config
|
from sanic.config import Config
|
||||||
from sanic.exceptions import RequestTimeout, ServiceUnavailable
|
from sanic.exceptions import RequestTimeout, ServiceUnavailable
|
||||||
from sanic.http import Http, Stage
|
from sanic.http import Http, Stage
|
||||||
from sanic.log import logger
|
from sanic.log import error_logger, logger
|
||||||
from sanic.models.protocol_types import TransportProtocol
|
from sanic.models.protocol_types import TransportProtocol
|
||||||
from sanic.request import Request
|
from sanic.request import Request
|
||||||
|
|
||||||
|
@ -199,11 +199,11 @@ class HttpProtocol(asyncio.Protocol):
|
||||||
except CancelledError:
|
except CancelledError:
|
||||||
pass
|
pass
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception("protocol.connection_task uncaught")
|
error_logger.exception("protocol.connection_task uncaught")
|
||||||
finally:
|
finally:
|
||||||
if self.app.debug and self._http:
|
if self.app.debug and self._http:
|
||||||
ip = self.transport.get_extra_info("peername")
|
ip = self.transport.get_extra_info("peername")
|
||||||
logger.error(
|
error_logger.error(
|
||||||
"Connection lost before response written"
|
"Connection lost before response written"
|
||||||
f" @ {ip} {self._http.request}"
|
f" @ {ip} {self._http.request}"
|
||||||
)
|
)
|
||||||
|
@ -212,7 +212,7 @@ class HttpProtocol(asyncio.Protocol):
|
||||||
try:
|
try:
|
||||||
self.close()
|
self.close()
|
||||||
except BaseException:
|
except BaseException:
|
||||||
logger.exception("Closing failed")
|
error_logger.exception("Closing failed")
|
||||||
|
|
||||||
async def receive_more(self):
|
async def receive_more(self):
|
||||||
"""
|
"""
|
||||||
|
@ -258,7 +258,7 @@ class HttpProtocol(asyncio.Protocol):
|
||||||
return
|
return
|
||||||
self._task.cancel()
|
self._task.cancel()
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception("protocol.check_timeouts")
|
error_logger.exception("protocol.check_timeouts")
|
||||||
|
|
||||||
async def send(self, data):
|
async def send(self, data):
|
||||||
"""
|
"""
|
||||||
|
@ -304,7 +304,7 @@ class HttpProtocol(asyncio.Protocol):
|
||||||
self.recv_buffer = bytearray()
|
self.recv_buffer = bytearray()
|
||||||
self.conn_info = ConnInfo(self.transport, unix=self._unix)
|
self.conn_info = ConnInfo(self.transport, unix=self._unix)
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception("protocol.connect_made")
|
error_logger.exception("protocol.connect_made")
|
||||||
|
|
||||||
def connection_lost(self, exc):
|
def connection_lost(self, exc):
|
||||||
try:
|
try:
|
||||||
|
@ -313,7 +313,7 @@ class HttpProtocol(asyncio.Protocol):
|
||||||
if self._task:
|
if self._task:
|
||||||
self._task.cancel()
|
self._task.cancel()
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception("protocol.connection_lost")
|
error_logger.exception("protocol.connection_lost")
|
||||||
|
|
||||||
def pause_writing(self):
|
def pause_writing(self):
|
||||||
self._can_write.clear()
|
self._can_write.clear()
|
||||||
|
@ -337,7 +337,7 @@ class HttpProtocol(asyncio.Protocol):
|
||||||
if self._data_received:
|
if self._data_received:
|
||||||
self._data_received.set()
|
self._data_received.set()
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception("protocol.data_received")
|
error_logger.exception("protocol.data_received")
|
||||||
|
|
||||||
|
|
||||||
def trigger_events(events: Optional[Iterable[Callable[..., Any]]], loop):
|
def trigger_events(events: Optional[Iterable[Callable[..., Any]]], loop):
|
||||||
|
@ -556,7 +556,7 @@ def serve(
|
||||||
try:
|
try:
|
||||||
http_server = loop.run_until_complete(server_coroutine)
|
http_server = loop.run_until_complete(server_coroutine)
|
||||||
except BaseException:
|
except BaseException:
|
||||||
logger.exception("Unable to start server")
|
error_logger.exception("Unable to start server")
|
||||||
return
|
return
|
||||||
|
|
||||||
trigger_events(after_start, loop)
|
trigger_events(after_start, loop)
|
||||||
|
|
|
@ -276,7 +276,7 @@ def test_handle_request_with_nested_sanic_exception(app, monkeypatch, caplog):
|
||||||
assert response.status == 500
|
assert response.status == 500
|
||||||
assert "Mock SanicException" in response.text
|
assert "Mock SanicException" in response.text
|
||||||
assert (
|
assert (
|
||||||
"sanic.root",
|
"sanic.error",
|
||||||
logging.ERROR,
|
logging.ERROR,
|
||||||
f"Exception occurred while handling uri: 'http://127.0.0.1:{port}/'",
|
f"Exception occurred while handling uri: 'http://127.0.0.1:{port}/'",
|
||||||
) in caplog.record_tuples
|
) in caplog.record_tuples
|
||||||
|
|
|
@ -113,9 +113,9 @@ def test_logging_pass_customer_logconfig():
|
||||||
def test_log_connection_lost(app, debug, monkeypatch):
|
def test_log_connection_lost(app, debug, monkeypatch):
|
||||||
""" Should not log Connection lost exception on non debug """
|
""" Should not log Connection lost exception on non debug """
|
||||||
stream = StringIO()
|
stream = StringIO()
|
||||||
root = logging.getLogger("sanic.root")
|
error = logging.getLogger("sanic.error")
|
||||||
root.addHandler(logging.StreamHandler(stream))
|
error.addHandler(logging.StreamHandler(stream))
|
||||||
monkeypatch.setattr(sanic.server, "logger", root)
|
monkeypatch.setattr(sanic.server, "error_logger", error)
|
||||||
|
|
||||||
@app.route("/conn_lost")
|
@app.route("/conn_lost")
|
||||||
async def conn_lost(request):
|
async def conn_lost(request):
|
||||||
|
|
|
@ -156,7 +156,7 @@ def test_middleware_response_raise_cancelled_error(app, caplog):
|
||||||
|
|
||||||
assert response.status == 503
|
assert response.status == 503
|
||||||
assert (
|
assert (
|
||||||
"sanic.root",
|
"sanic.error",
|
||||||
logging.ERROR,
|
logging.ERROR,
|
||||||
"Exception occurred while handling uri: 'http://127.0.0.1:42101/'",
|
"Exception occurred while handling uri: 'http://127.0.0.1:42101/'",
|
||||||
) not in caplog.record_tuples
|
) not in caplog.record_tuples
|
||||||
|
@ -174,7 +174,7 @@ def test_middleware_response_raise_exception(app, caplog):
|
||||||
assert response.status == 404
|
assert response.status == 404
|
||||||
# 404 errors are not logged
|
# 404 errors are not logged
|
||||||
assert (
|
assert (
|
||||||
"sanic.root",
|
"sanic.error",
|
||||||
logging.ERROR,
|
logging.ERROR,
|
||||||
"Exception occurred while handling uri: 'http://127.0.0.1:42101/'",
|
"Exception occurred while handling uri: 'http://127.0.0.1:42101/'",
|
||||||
) not in caplog.record_tuples
|
) not in caplog.record_tuples
|
||||||
|
|
Loading…
Reference in New Issue
Block a user