update logging placement

This commit is contained in:
Raphael Deem 2017-01-17 15:38:20 -08:00
parent 9108a4c69f
commit ba1e006585
3 changed files with 16 additions and 10 deletions

View File

@ -14,7 +14,7 @@ logging.basicConfig(
log = logging.getLogger() log = logging.getLogger()
# Set logger to override default basicConfig # Set logger to override default basicConfig
sanic = Sanic(logger=True) sanic = Sanic()
@sanic.route("/") @sanic.route("/")
def test(request): def test(request):
log.info("received request; responding with 'hey'") log.info("received request; responding with 'hey'")

View File

@ -21,12 +21,7 @@ from os import set_inheritable
class Sanic: class Sanic:
def __init__(self, name=None, router=None, def __init__(self, name=None, router=None,
error_handler=None, logger=None): error_handler=None):
if logger is None:
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s: %(levelname)s: %(message)s"
)
if name is None: if name is None:
frame_records = stack()[1] frame_records = stack()[1]
name = getmodulename(frame_records[1]) name = getmodulename(frame_records[1])
@ -154,7 +149,8 @@ class Sanic:
def register_blueprint(self, *args, **kwargs): def register_blueprint(self, *args, **kwargs):
# TODO: deprecate 1.0 # TODO: deprecate 1.0
log.warning("Use of register_blueprint will be deprecated in " log.warning("Use of register_blueprint will be deprecated in "
"version 1.0. Please use the blueprint method instead") "version 1.0. Please use the blueprint method instead",
DeprecationWarning)
return self.blueprint(*args, **kwargs) return self.blueprint(*args, **kwargs)
# -------------------------------------------------------------------- # # -------------------------------------------------------------------- #
@ -247,7 +243,7 @@ class Sanic:
def run(self, host="127.0.0.1", port=8000, debug=False, before_start=None, def run(self, host="127.0.0.1", port=8000, debug=False, before_start=None,
after_start=None, before_stop=None, after_stop=None, sock=None, after_start=None, before_stop=None, after_stop=None, sock=None,
workers=1, loop=None, protocol=HttpProtocol, backlog=100, workers=1, loop=None, protocol=HttpProtocol, backlog=100,
stop_event=None): stop_event=None, logger=None):
""" """
Runs the HTTP Server and listens until keyboard interrupt or term Runs the HTTP Server and listens until keyboard interrupt or term
signal. On termination, drains connections before closing. signal. On termination, drains connections before closing.
@ -269,6 +265,10 @@ class Sanic:
:param protocol: Subclass of asyncio protocol class :param protocol: Subclass of asyncio protocol class
:return: Nothing :return: Nothing
""" """
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s: %(levelname)s: %(message)s"
)
self.error_handler.debug = True self.error_handler.debug = True
self.debug = debug self.debug = debug
self.loop = loop self.loop = loop
@ -350,6 +350,12 @@ class Sanic:
:param stop_event: if provided, is used as a stop signal :param stop_event: if provided, is used as a stop signal
:return: :return:
""" """
# In case this is called directly, we configure logging here too.
# This won't interfere with the same call from run()
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s: %(levelname)s: %(message)s"
)
server_settings['reuse_port'] = True server_settings['reuse_port'] = True
# Create a stop event to be triggered by a signal # Create a stop event to be triggered by a signal

View File

@ -19,7 +19,7 @@ def test_log():
stream=log_stream stream=log_stream
) )
log = logging.getLogger() log = logging.getLogger()
app = Sanic('test_logging', logger=True) app = Sanic('test_logging')
@app.route('/') @app.route('/')
def handler(request): def handler(request):
log.info('hello world') log.info('hello world')