change to use default python config code

This commit is contained in:
zenix 2017-04-12 18:44:47 +09:00
parent 5f0e05f3bf
commit 36d4d85849
7 changed files with 93 additions and 98 deletions

View File

@ -9,4 +9,3 @@ pytest
tox tox
ujson ujson
uvloop uvloop
pyyaml

View File

@ -3,4 +3,3 @@ httptools
ujson ujson
uvloop uvloop
websockets websockets
pyyaml

View File

@ -2,8 +2,6 @@ import logging
import logging.config import logging.config
import re import re
import warnings import warnings
import os
import yaml
from asyncio import get_event_loop, ensure_future, CancelledError from asyncio import get_event_loop, ensure_future, CancelledError
from collections import deque, defaultdict from collections import deque, defaultdict
from functools import partial from functools import partial
@ -12,8 +10,7 @@ from traceback import format_exc
from urllib.parse import urlencode, urlunparse from urllib.parse import urlencode, urlunparse
from ssl import create_default_context, Purpose from ssl import create_default_context, Purpose
from sanic import __path__ as lib_path from sanic.config import Config, DEFAULT_LOG_CONF
from sanic.config import Config
from sanic.constants import HTTP_METHODS from sanic.constants import HTTP_METHODS
from sanic.exceptions import ServerError, URLBuildError, SanicException from sanic.exceptions import ServerError, URLBuildError, SanicException
from sanic.handlers import ErrorHandler from sanic.handlers import ErrorHandler
@ -31,16 +28,12 @@ class Sanic:
def __init__(self, name=None, router=None, error_handler=None, def __init__(self, name=None, router=None, error_handler=None,
load_env=True, request_class=None, load_env=True, request_class=None,
log_config_path=os.path.join(lib_path[0], "default.conf")): log_config=DEFAULT_LOG_CONF):
conf = None if log_config:
if log_config_path and os.path.exists(log_config_path): logging.config.dictConfig(log_config)
with open(log_config_path) as f: else:
conf = yaml.load(f)
conf.setdefault('version', 1)
logging.config.dictConfig(conf)
# Only set up a default log handler if the # Only set up a default log handler if the
# end-user application didn't set anything up. # end-user application didn't set anything up.
if not conf and log.level == logging.NOTSET:
formatter = logging.Formatter( formatter = logging.Formatter(
"%(asctime)s: %(levelname)s: %(message)s") "%(asctime)s: %(levelname)s: %(message)s")
handler = logging.StreamHandler() handler = logging.StreamHandler()
@ -58,7 +51,7 @@ class Sanic:
self.request_class = request_class self.request_class = request_class
self.error_handler = error_handler or ErrorHandler() self.error_handler = error_handler or ErrorHandler()
self.config = Config(load_env=load_env) self.config = Config(load_env=load_env)
self.log_config_path = log_config_path self.log_config = log_config
self.request_middleware = deque() self.request_middleware = deque()
self.response_middleware = deque() self.response_middleware = deque()
self.blueprints = {} self.blueprints = {}
@ -525,7 +518,7 @@ class Sanic:
after_start=None, before_stop=None, after_stop=None, ssl=None, after_start=None, before_stop=None, after_stop=None, ssl=None,
sock=None, workers=1, loop=None, protocol=None, sock=None, workers=1, loop=None, protocol=None,
backlog=100, stop_event=None, register_sys_signals=True, 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 """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.
@ -552,11 +545,8 @@ class Sanic:
:param protocol: Subclass of asyncio protocol class :param protocol: Subclass of asyncio protocol class
:return: Nothing :return: Nothing
""" """
if log_config_path and os.path.exists(log_config_path): if log_config:
with open(log_config_path) as f: logging.config.dictConfig(log_config)
conf = yaml.load(f)
conf.setdefault('version', 1)
logging.config.dictConfig(conf)
if protocol is None: if protocol is None:
protocol = (WebSocketProtocol if self.websocket_enabled protocol = (WebSocketProtocol if self.websocket_enabled
else HttpProtocol) else HttpProtocol)
@ -571,7 +561,7 @@ class Sanic:
after_stop=after_stop, ssl=ssl, sock=sock, workers=workers, after_stop=after_stop, ssl=ssl, sock=sock, workers=workers,
loop=loop, protocol=protocol, backlog=backlog, loop=loop, protocol=protocol, backlog=backlog,
register_sys_signals=register_sys_signals, register_sys_signals=register_sys_signals,
has_log_file=log_config_path is not None) has_log=log_config is not None)
try: try:
self.is_running = True self.is_running = True
@ -599,18 +589,14 @@ class Sanic:
before_stop=None, after_stop=None, ssl=None, before_stop=None, after_stop=None, ssl=None,
sock=None, loop=None, protocol=None, sock=None, loop=None, protocol=None,
backlog=100, stop_event=None, backlog=100, stop_event=None,
log_config_path=os.path.join(lib_path[0], log_config=DEFAULT_LOG_CONF):
"default.yml")):
"""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
way to run a Sanic application. way to run a Sanic application.
""" """
if log_config_path and os.path.exists(log_config_path): if log_config:
with open(log_config_path) as f: logging.config.dictConfig(log_config)
conf = yaml.load(f)
conf.setdefault('version', 1)
logging.config.dictConfig(conf)
if protocol is None: if protocol is None:
protocol = (WebSocketProtocol if self.websocket_enabled protocol = (WebSocketProtocol if self.websocket_enabled
else HttpProtocol) else HttpProtocol)
@ -625,7 +611,7 @@ class Sanic:
after_stop=after_stop, ssl=ssl, sock=sock, after_stop=after_stop, ssl=ssl, sock=sock,
loop=loop or get_event_loop(), protocol=protocol, loop=loop or get_event_loop(), protocol=protocol,
backlog=backlog, run_async=True, 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) return await serve(**server_settings)
@ -655,7 +641,7 @@ class Sanic:
before_start=None, after_start=None, before_stop=None, before_start=None, after_start=None, before_stop=None,
after_stop=None, ssl=None, sock=None, workers=1, loop=None, after_stop=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_file=True): register_sys_signals=True, run_async=False, has_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):
@ -708,7 +694,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_file': has_log_file 'has_log': has_log
} }
# -------------------------------------------- # # -------------------------------------------- #

View File

@ -1,10 +1,75 @@
from sanic.defaultFilter import DefaultFilter
import os import os
import types import types
SANIC_PREFIX = 'SANIC_' 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): class Config(dict):
def __init__(self, defaults=None, load_env=True): def __init__(self, defaults=None, load_env=True):
super().__init__(defaults or {}) super().__init__(defaults or {})

View File

@ -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]

View File

@ -67,13 +67,13 @@ class HttpProtocol(asyncio.Protocol):
'request_handler', 'request_timeout', 'request_max_size', 'request_handler', 'request_timeout', 'request_max_size',
'request_class', 'request_class',
# enable or disable access log / error log purpose # enable or disable access log / error log purpose
'has_log_file', 'has_log',
# connection management # connection management
'_total_request_size', '_timeout_handler', '_last_communication_time') '_total_request_size', '_timeout_handler', '_last_communication_time')
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_file=True): request_max_size=None, request_class=None, has_log=True):
self.loop = loop self.loop = loop
self.transport = None self.transport = None
self.request = None self.request = None
@ -81,7 +81,7 @@ class HttpProtocol(asyncio.Protocol):
self.url = None self.url = None
self.headers = None self.headers = None
self.signal = signal self.signal = signal
self.has_log_file = has_log_file self.has_log = has_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
@ -193,7 +193,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_file: if self.has_log:
netlog.info('', extra={ netlog.info('', extra={
'status': response.status, 'status': response.status,
'byte': len(response.body), 'byte': len(response.body),
@ -236,7 +236,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_file: if self.has_log:
netlog.info('', extra={ netlog.info('', extra={
'status': response.status, 'status': response.status,
'byte': -1, 'byte': -1,
@ -279,7 +279,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_file: if self.has_log:
extra = { extra = {
'status': response.status, 'status': response.status,
'host': '', '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, 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_file=True): signal=Signal(), request_class=None, has_log=True):
"""Start asynchronous HTTP Server on an individual process. """Start asynchronous HTTP Server on an individual process.
:param host: Address to host on :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 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_file: disable/enable access log and error log :param has_log: disable/enable access log and error log
:return: Nothing :return: Nothing
""" """
if not run_async: if not run_async:
@ -406,7 +406,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_file=has_log_file has_log=has_log
) )
server_coroutine = loop.create_server( server_coroutine = loop.create_server(

View File

@ -27,7 +27,6 @@ setup_kwargs = {
'description': ( 'description': (
'A microframework based on uvloop, httptools, and learnings of flask'), 'A microframework based on uvloop, httptools, and learnings of flask'),
'packages': ['sanic'], 'packages': ['sanic'],
'package_data': {'':['default.yml']},
'platforms': 'any', 'platforms': 'any',
'classifiers': [ 'classifiers': [
'Development Status :: 2 - Pre-Alpha', 'Development Status :: 2 - Pre-Alpha',
@ -47,7 +46,6 @@ requirements = [
ujson, ujson,
'aiofiles>=0.3.0', 'aiofiles>=0.3.0',
'websockets>=3.2', 'websockets>=3.2',
'pyyaml>=3.12'
] ]
if strtobool(os.environ.get("SANIC_NO_UJSON", "no")): if strtobool(os.environ.get("SANIC_NO_UJSON", "no")):
print("Installing without uJSON") print("Installing without uJSON")