From 36d4d85849f36dda9ce14217ed24ecb91c8a32e0 Mon Sep 17 00:00:00 2001 From: zenix Date: Wed, 12 Apr 2017 18:44:47 +0900 Subject: [PATCH] change to use default python config code --- requirements-dev.txt | 1 - requirements.txt | 1 - sanic/app.py | 50 ++++++++++++--------------------- sanic/config.py | 67 +++++++++++++++++++++++++++++++++++++++++++- sanic/default.yml | 52 ---------------------------------- sanic/server.py | 18 ++++++------ setup.py | 2 -- 7 files changed, 93 insertions(+), 98 deletions(-) delete mode 100644 sanic/default.yml diff --git a/requirements-dev.txt b/requirements-dev.txt index 7c06ed28..163df025 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -9,4 +9,3 @@ pytest tox ujson uvloop -pyyaml diff --git a/requirements.txt b/requirements.txt index 49da374e..e370b52f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,3 @@ httptools ujson uvloop websockets -pyyaml diff --git a/sanic/app.py b/sanic/app.py index 0d048c35..7da1dc41 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -2,8 +2,6 @@ import logging import logging.config import re import warnings -import os -import yaml from asyncio import get_event_loop, ensure_future, CancelledError from collections import deque, defaultdict from functools import partial @@ -12,8 +10,7 @@ from traceback import format_exc from urllib.parse import urlencode, urlunparse from ssl import create_default_context, Purpose -from sanic import __path__ as lib_path -from sanic.config import Config +from sanic.config import Config, DEFAULT_LOG_CONF from sanic.constants import HTTP_METHODS from sanic.exceptions import ServerError, URLBuildError, SanicException from sanic.handlers import ErrorHandler @@ -31,16 +28,12 @@ class Sanic: def __init__(self, name=None, router=None, error_handler=None, load_env=True, request_class=None, - log_config_path=os.path.join(lib_path[0], "default.conf")): - conf = None - if log_config_path and os.path.exists(log_config_path): - with open(log_config_path) as f: - conf = yaml.load(f) - conf.setdefault('version', 1) - logging.config.dictConfig(conf) - # Only set up a default log handler if the - # end-user application didn't set anything up. - if not conf and log.level == logging.NOTSET: + log_config=DEFAULT_LOG_CONF): + if log_config: + logging.config.dictConfig(log_config) + else: + # Only set up a default log handler if the + # end-user application didn't set anything up. formatter = logging.Formatter( "%(asctime)s: %(levelname)s: %(message)s") handler = logging.StreamHandler() @@ -58,7 +51,7 @@ class Sanic: self.request_class = request_class self.error_handler = error_handler or ErrorHandler() self.config = Config(load_env=load_env) - self.log_config_path = log_config_path + self.log_config = log_config self.request_middleware = deque() self.response_middleware = deque() self.blueprints = {} @@ -525,7 +518,7 @@ class Sanic: after_start=None, before_stop=None, after_stop=None, ssl=None, sock=None, workers=1, loop=None, protocol=None, backlog=100, stop_event=None, register_sys_signals=True, - log_config_path=os.path.join(lib_path[0], "default.yml")): + log_config=DEFAULT_LOG_CONF): """Run the HTTP Server and listen until keyboard interrupt or term signal. On termination, drain connections before closing. @@ -552,11 +545,8 @@ class Sanic: :param protocol: Subclass of asyncio protocol class :return: Nothing """ - if log_config_path and os.path.exists(log_config_path): - with open(log_config_path) as f: - conf = yaml.load(f) - conf.setdefault('version', 1) - logging.config.dictConfig(conf) + if log_config: + logging.config.dictConfig(log_config) if protocol is None: protocol = (WebSocketProtocol if self.websocket_enabled else HttpProtocol) @@ -571,7 +561,7 @@ class Sanic: after_stop=after_stop, ssl=ssl, sock=sock, workers=workers, loop=loop, protocol=protocol, backlog=backlog, register_sys_signals=register_sys_signals, - has_log_file=log_config_path is not None) + has_log=log_config is not None) try: self.is_running = True @@ -599,18 +589,14 @@ class Sanic: before_stop=None, after_stop=None, ssl=None, sock=None, loop=None, protocol=None, backlog=100, stop_event=None, - log_config_path=os.path.join(lib_path[0], - "default.yml")): + log_config=DEFAULT_LOG_CONF): """Asynchronous version of `run`. NOTE: This does not support multiprocessing and is not the preferred way to run a Sanic application. """ - if log_config_path and os.path.exists(log_config_path): - with open(log_config_path) as f: - conf = yaml.load(f) - conf.setdefault('version', 1) - logging.config.dictConfig(conf) + if log_config: + logging.config.dictConfig(log_config) if protocol is None: protocol = (WebSocketProtocol if self.websocket_enabled else HttpProtocol) @@ -625,7 +611,7 @@ class Sanic: after_stop=after_stop, ssl=ssl, sock=sock, loop=loop or get_event_loop(), protocol=protocol, backlog=backlog, run_async=True, - has_log_file=log_config_path is not None) + has_log=log_config is not None) return await serve(**server_settings) @@ -655,7 +641,7 @@ class Sanic: before_start=None, after_start=None, before_stop=None, after_stop=None, ssl=None, sock=None, workers=1, loop=None, protocol=HttpProtocol, backlog=100, stop_event=None, - register_sys_signals=True, run_async=False, has_log_file=True): + register_sys_signals=True, run_async=False, has_log=True): """Helper function used by `run` and `create_server`.""" if isinstance(ssl, dict): @@ -708,7 +694,7 @@ class Sanic: 'loop': loop, 'register_sys_signals': register_sys_signals, 'backlog': backlog, - 'has_log_file': has_log_file + 'has_log': has_log } # -------------------------------------------- # diff --git a/sanic/config.py b/sanic/config.py index 406c44e6..21155bad 100644 --- a/sanic/config.py +++ b/sanic/config.py @@ -1,10 +1,75 @@ +from sanic.defaultFilter import DefaultFilter import os - import types SANIC_PREFIX = 'SANIC_' +DEFAULT_LOG_CONF = { + 'version': 1, + 'filters': { + 'access_filter': { + '()': DefaultFilter, + 'param': [0, 10, 20] + }, + 'error_filter': { + '()': DefaultFilter, + 'param': [30, 40, 50] + } + }, + 'formatters': { + 'simple': { + 'format': '%(asctime)s - (%(name)s)[%(levelname)s]: %(message)s', + 'datefmt': '%Y-%m-%d %H:%M:%S' + }, + 'access': { + 'format': '%(asctime)s - [%(levelname)s][%(host)s]: ' + + '%(request)s %(message)s %(status)d %(byte)d', + 'datefmt': '%Y-%m-%d %H:%M:%S' + } + }, + 'handlers': { + 'internal': { + 'class': 'logging.handlers.TimedRotatingFileHandler', + 'filters': ['access_filter'], + 'formatter': 'simple', + 'when': 'D', + 'interval': 1, + 'backupCount': 7, + 'filename': 'access.log' + }, + 'access': { + 'class': 'logging.handlers.TimedRotatingFileHandler', + 'filters': ['access_filter'], + 'formatter': 'access', + 'when': 'D', + 'interval': 1, + 'backupCount': 7, + 'filename': 'access.log' + }, + 'error': { + 'class': 'logging.handlers.TimedRotatingFileHandler', + 'filters': ['error_filter'], + 'when': 'D', + 'interval': 1, + 'backupCount': 7, + 'filename': 'error.log', + 'formatter': 'simple' + } + }, + 'loggers': { + 'sanic': { + 'level': 'DEBUG', + 'handlers': ['internal', 'error'] + }, + 'network': { + 'level': 'DEBUG', + 'handlers': ['access', 'error'] + } + } +} + + class Config(dict): def __init__(self, defaults=None, load_env=True): super().__init__(defaults or {}) diff --git a/sanic/default.yml b/sanic/default.yml deleted file mode 100644 index af69cd80..00000000 --- a/sanic/default.yml +++ /dev/null @@ -1,52 +0,0 @@ -version: 1 -filters: - access_filter: - (): sanic.defaultFilter.DefaultFilter - param: [0, 10, 20] - error_filter: - (): sanic.defaultFilter.DefaultFilter - param: [30, 40, 50] - -formatters: - simple: - format: '%(asctime)s - (%(name)s)[%(levelname)s]: %(message)s' - datefmt: '%Y-%m-%d %H:%M:%S' - access: - format: '%(asctime)s - [%(levelname)s][%(host)s]: %(request)s %(message)s %(status)s %(byte)d' - datefmt: '%Y-%m-%d %H:%M:%S' - -handlers: - internal: - class: logging.handlers.TimedRotatingFileHandler - filters: [access_filter] - formatter: simple - when: 'D' - interval: 1 - backupCount: 7 - filename: 'access.log' - - access: - class: logging.handlers.TimedRotatingFileHandler - filters: [access_filter] - formatter: access - when: 'D' - interval: 1 - backupCount: 7 - filename: 'access.log' - - error: - class: logging.handlers.TimedRotatingFileHandler - filters: [error_filter] - filename: 'error.log' - when: 'D' - interval: 1 - backupCount: 7 - formatter: simple - -loggers: - sanic: - level: DEBUG - handlers: [internal, error] - network: - level: DEBUG - handlers: [access, error] diff --git a/sanic/server.py b/sanic/server.py index bdae20c4..7da809a5 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -67,13 +67,13 @@ class HttpProtocol(asyncio.Protocol): 'request_handler', 'request_timeout', 'request_max_size', 'request_class', # enable or disable access log / error log purpose - 'has_log_file', + 'has_log', # connection management '_total_request_size', '_timeout_handler', '_last_communication_time') def __init__(self, *, loop, request_handler, error_handler, signal=Signal(), connections=set(), request_timeout=60, - request_max_size=None, request_class=None, has_log_file=True): + request_max_size=None, request_class=None, has_log=True): self.loop = loop self.transport = None self.request = None @@ -81,7 +81,7 @@ class HttpProtocol(asyncio.Protocol): self.url = None self.headers = None self.signal = signal - self.has_log_file = has_log_file + self.has_log = has_log self.connections = connections self.request_handler = request_handler self.error_handler = error_handler @@ -193,7 +193,7 @@ class HttpProtocol(asyncio.Protocol): response.output( self.request.version, keep_alive, self.request_timeout)) - if self.has_log_file: + if self.has_log: netlog.info('', extra={ 'status': response.status, 'byte': len(response.body), @@ -236,7 +236,7 @@ class HttpProtocol(asyncio.Protocol): response.transport = self.transport await response.stream( self.request.version, keep_alive, self.request_timeout) - if self.has_log_file: + if self.has_log: netlog.info('', extra={ 'status': response.status, 'byte': -1, @@ -279,7 +279,7 @@ class HttpProtocol(asyncio.Protocol): "Writing error failed, connection closed {}".format(repr(e)), from_error=True) finally: - if self.has_log_file: + if self.has_log: extra = { 'status': response.status, 'host': '', @@ -357,7 +357,7 @@ def serve(host, port, request_handler, error_handler, before_start=None, request_timeout=60, ssl=None, sock=None, request_max_size=None, reuse_port=False, loop=None, protocol=HttpProtocol, backlog=100, register_sys_signals=True, run_async=False, connections=None, - signal=Signal(), request_class=None, has_log_file=True): + signal=Signal(), request_class=None, has_log=True): """Start asynchronous HTTP Server on an individual process. :param host: Address to host on @@ -383,7 +383,7 @@ def serve(host, port, request_handler, error_handler, before_start=None, :param loop: asyncio compatible event loop :param protocol: subclass of asyncio protocol class :param request_class: Request class to use - :param has_log_file: disable/enable access log and error log + :param has_log: disable/enable access log and error log :return: Nothing """ if not run_async: @@ -406,7 +406,7 @@ def serve(host, port, request_handler, error_handler, before_start=None, request_timeout=request_timeout, request_max_size=request_max_size, request_class=request_class, - has_log_file=has_log_file + has_log=has_log ) server_coroutine = loop.create_server( diff --git a/setup.py b/setup.py index e87afdc1..deb52c27 100644 --- a/setup.py +++ b/setup.py @@ -27,7 +27,6 @@ setup_kwargs = { 'description': ( 'A microframework based on uvloop, httptools, and learnings of flask'), 'packages': ['sanic'], - 'package_data': {'':['default.yml']}, 'platforms': 'any', 'classifiers': [ 'Development Status :: 2 - Pre-Alpha', @@ -47,7 +46,6 @@ requirements = [ ujson, 'aiofiles>=0.3.0', 'websockets>=3.2', - 'pyyaml>=3.12' ] if strtobool(os.environ.get("SANIC_NO_UJSON", "no")): print("Installing without uJSON")