def setup_logging(level):
"""
Makes stdout and stderr unicode friendly in case of misconfigured
environments, initializes the logging and enables colored logs if it is
appropriate.
:param level: The logging level, can be either an int or a string.
:return: None
"""
if not isinstance(level, int):
level = logging._nameToLevel[level]
def ensure_utf8_stream(stream):
if not isinstance(stream, io.StringIO) and hasattr(stream, "buffer"):
stream = codecs.getwriter("utf-8")(stream.buffer)
stream.encoding = "utf-8"
return stream
sys.stdout, sys.stderr = (ensure_utf8_stream(s)
for s in (sys.stdout, sys.stderr))
logging.basicConfig(level=level)
root = logging.getLogger()
# In some cases root has handlers and basicConfig() is a no-op
root.setLevel(level)
if not sys.stdin.closed and sys.stdout.isatty():
handler = root.handlers[0]
handler.setFormatter(ColorFormatter())
评论列表
文章目录