Make it easier to override logging
Take influence from how Werkzeug configures logging by only configuring a handler if no root handlers were previously configured by the end user.
This commit is contained in:
parent
bef34d66f5
commit
72fba62e09
|
@ -1,3 +1,3 @@
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger('sanic')
|
||||||
|
|
|
@ -22,6 +22,15 @@ 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):
|
error_handler=None):
|
||||||
|
# Only set up a default log handler if the
|
||||||
|
# end-user application didn't set anything up.
|
||||||
|
if not logging.root.handlers and log.level == logging.NOTSET:
|
||||||
|
formatter = logging.Formatter(
|
||||||
|
"%(asctime)s: %(levelname)s: %(message)s")
|
||||||
|
handler = logging.StreamHandler()
|
||||||
|
handler.setFormatter(formatter)
|
||||||
|
log.addHandler(handler)
|
||||||
|
log.setLevel(logging.INFO)
|
||||||
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])
|
||||||
|
@ -273,10 +282,6 @@ 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
|
||||||
|
@ -364,12 +369,6 @@ 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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user