use access_log as param

This commit is contained in:
Yun Xu 2017-09-10 18:38:52 -07:00
parent c9a40c180a
commit c9cbc00e36
2 changed files with 16 additions and 21 deletions

View File

@ -567,7 +567,7 @@ class Sanic:
def run(self, host=None, port=None, debug=False, ssl=None, def run(self, host=None, port=None, debug=False, ssl=None,
sock=None, workers=1, protocol=None, sock=None, workers=1, protocol=None,
backlog=100, stop_event=None, register_sys_signals=True, backlog=100, stop_event=None, register_sys_signals=True,
log_config=None): access_log=True):
"""Run the HTTP Server and listen until keyboard interrupt or term """Run the HTTP Server and listen until keyboard interrupt or term
signal. On termination, drain connections before closing. signal. On termination, drain connections before closing.
@ -588,9 +588,6 @@ class Sanic:
if sock is None: if sock is None:
host, port = host or "127.0.0.1", port or 8000 host, port = host or "127.0.0.1", port or 8000
if log_config:
self.log_config = log_config
logging.config.dictConfig(log_config)
if protocol is None: if protocol is None:
protocol = (WebSocketProtocol if self.websocket_enabled protocol = (WebSocketProtocol if self.websocket_enabled
else HttpProtocol) else HttpProtocol)
@ -603,7 +600,7 @@ class Sanic:
host=host, port=port, debug=debug, ssl=ssl, sock=sock, host=host, port=port, debug=debug, ssl=ssl, sock=sock,
workers=workers, protocol=protocol, backlog=backlog, workers=workers, protocol=protocol, backlog=backlog,
register_sys_signals=register_sys_signals, register_sys_signals=register_sys_signals,
has_log=self.log_config is not None) access_log=access_log)
try: try:
self.is_running = True self.is_running = True
@ -630,7 +627,7 @@ class Sanic:
async def create_server(self, host=None, port=None, debug=False, async def create_server(self, host=None, port=None, debug=False,
ssl=None, sock=None, protocol=None, ssl=None, sock=None, protocol=None,
backlog=100, stop_event=None, backlog=100, stop_event=None,
log_config=None): access_log=True):
"""Asynchronous version of `run`. """Asynchronous version of `run`.
NOTE: This does not support multiprocessing and is not the preferred NOTE: This does not support multiprocessing and is not the preferred
@ -639,8 +636,6 @@ class Sanic:
if sock is None: if sock is None:
host, port = host or "127.0.0.1", port or 8000 host, port = host or "127.0.0.1", port or 8000
if log_config:
logging.config.dictConfig(log_config)
if protocol is None: if protocol is None:
protocol = (WebSocketProtocol if self.websocket_enabled protocol = (WebSocketProtocol if self.websocket_enabled
else HttpProtocol) else HttpProtocol)
@ -654,7 +649,7 @@ class Sanic:
host=host, port=port, debug=debug, ssl=ssl, sock=sock, host=host, port=port, debug=debug, ssl=ssl, sock=sock,
loop=get_event_loop(), protocol=protocol, loop=get_event_loop(), protocol=protocol,
backlog=backlog, run_async=True, backlog=backlog, run_async=True,
has_log=log_config is not None) access_log=access_log)
# Trigger before_start events # Trigger before_start events
await self.trigger_events( await self.trigger_events(
@ -699,7 +694,7 @@ class Sanic:
def _helper(self, host=None, port=None, debug=False, def _helper(self, host=None, port=None, debug=False,
ssl=None, sock=None, workers=1, loop=None, ssl=None, sock=None, workers=1, loop=None,
protocol=HttpProtocol, backlog=100, stop_event=None, protocol=HttpProtocol, backlog=100, stop_event=None,
register_sys_signals=True, run_async=False, has_log=True): register_sys_signals=True, run_async=False, access_log=True):
"""Helper function used by `run` and `create_server`.""" """Helper function used by `run` and `create_server`."""
if isinstance(ssl, dict): if isinstance(ssl, dict):
# try common aliaseses # try common aliaseses
@ -738,7 +733,7 @@ class Sanic:
'loop': loop, 'loop': loop,
'register_sys_signals': register_sys_signals, 'register_sys_signals': register_sys_signals,
'backlog': backlog, 'backlog': backlog,
'has_log': has_log, 'access_log': access_log,
'websocket_max_size': self.config.WEBSOCKET_MAX_SIZE, 'websocket_max_size': self.config.WEBSOCKET_MAX_SIZE,
'websocket_max_queue': self.config.WEBSOCKET_MAX_QUEUE, 'websocket_max_queue': self.config.WEBSOCKET_MAX_QUEUE,
'graceful_shutdown_timeout': self.config.GRACEFUL_SHUTDOWN_TIMEOUT 'graceful_shutdown_timeout': self.config.GRACEFUL_SHUTDOWN_TIMEOUT

View File

@ -65,15 +65,15 @@ class HttpProtocol(asyncio.Protocol):
# request config # request config
'request_handler', 'request_timeout', 'request_max_size', 'request_handler', 'request_timeout', 'request_max_size',
'request_class', 'is_request_stream', 'router', 'request_class', 'is_request_stream', 'router',
# enable or disable access log / error log purpose # enable or disable access log purpose
'has_log', 'access_log',
# connection management # connection management
'_total_request_size', '_timeout_handler', '_last_communication_time', '_total_request_size', '_timeout_handler', '_last_communication_time',
'_is_stream_handler') '_is_stream_handler')
def __init__(self, *, loop, request_handler, error_handler, def __init__(self, *, loop, request_handler, error_handler,
signal=Signal(), connections=set(), request_timeout=60, signal=Signal(), connections=set(), request_timeout=60,
request_max_size=None, request_class=None, has_log=True, request_max_size=None, request_class=None, access_log=True,
keep_alive=True, is_request_stream=False, router=None, keep_alive=True, is_request_stream=False, router=None,
state=None, debug=False, **kwargs): state=None, debug=False, **kwargs):
self.loop = loop self.loop = loop
@ -84,7 +84,7 @@ class HttpProtocol(asyncio.Protocol):
self.headers = None self.headers = None
self.router = router self.router = router
self.signal = signal self.signal = signal
self.has_log = has_log self.access_log = access_log
self.connections = connections self.connections = connections
self.request_handler = request_handler self.request_handler = request_handler
self.error_handler = error_handler self.error_handler = error_handler
@ -246,7 +246,7 @@ class HttpProtocol(asyncio.Protocol):
response.output( response.output(
self.request.version, keep_alive, self.request.version, keep_alive,
self.request_timeout)) self.request_timeout))
if self.has_log: if self.access_log:
netlog.info('', extra={ netlog.info('', extra={
'status': response.status, 'status': response.status,
'byte': len(response.body), 'byte': len(response.body),
@ -288,7 +288,7 @@ class HttpProtocol(asyncio.Protocol):
response.transport = self.transport response.transport = self.transport
await response.stream( await response.stream(
self.request.version, keep_alive, self.request_timeout) self.request.version, keep_alive, self.request_timeout)
if self.has_log: if self.access_log:
netlog.info('', extra={ netlog.info('', extra={
'status': response.status, 'status': response.status,
'byte': -1, 'byte': -1,
@ -333,7 +333,7 @@ class HttpProtocol(asyncio.Protocol):
"Writing error failed, connection closed {}".format(repr(e)), "Writing error failed, connection closed {}".format(repr(e)),
from_error=True) from_error=True)
finally: finally:
if self.has_log: if self.access_log:
extra = dict() extra = dict()
if isinstance(response, HTTPResponse): if isinstance(response, HTTPResponse):
extra['status'] = response.status extra['status'] = response.status
@ -424,7 +424,7 @@ def serve(host, port, request_handler, error_handler, before_start=None,
request_timeout=60, ssl=None, sock=None, request_max_size=None, request_timeout=60, ssl=None, sock=None, request_max_size=None,
reuse_port=False, loop=None, protocol=HttpProtocol, backlog=100, reuse_port=False, loop=None, protocol=HttpProtocol, backlog=100,
register_sys_signals=True, run_async=False, connections=None, register_sys_signals=True, run_async=False, connections=None,
signal=Signal(), request_class=None, has_log=True, keep_alive=True, signal=Signal(), request_class=None, access_log=True, keep_alive=True,
is_request_stream=False, router=None, websocket_max_size=None, is_request_stream=False, router=None, websocket_max_size=None,
websocket_max_queue=None, state=None, websocket_max_queue=None, state=None,
graceful_shutdown_timeout=15.0): graceful_shutdown_timeout=15.0):
@ -453,7 +453,7 @@ def serve(host, port, request_handler, error_handler, before_start=None,
:param loop: asyncio compatible event loop :param loop: asyncio compatible event loop
:param protocol: subclass of asyncio protocol class :param protocol: subclass of asyncio protocol class
:param request_class: Request class to use :param request_class: Request class to use
:param has_log: disable/enable access log and error log :param access_log: disable/enable access log
:param is_request_stream: disable/enable Request.stream :param is_request_stream: disable/enable Request.stream
:param router: Router object :param router: Router object
:return: Nothing :return: Nothing
@ -476,7 +476,7 @@ def serve(host, port, request_handler, error_handler, before_start=None,
request_timeout=request_timeout, request_timeout=request_timeout,
request_max_size=request_max_size, request_max_size=request_max_size,
request_class=request_class, request_class=request_class,
has_log=has_log, access_log=access_log,
keep_alive=keep_alive, keep_alive=keep_alive,
is_request_stream=is_request_stream, is_request_stream=is_request_stream,
router=router, router=router,