在Flask框架中,每处理完一条请求,都会在终端打印出一条请求的日志信息:
127.0.0.1 - - [31/May/2018 04:40:35] "POST /xxx/xxx/xxx HTTP/1.1" 200 -
之所以打印了这条日志信息,是因为Flask框架在处理完请求后,调用了werkzeug库的_log函数,函数如下。
def _log(type, message, *args, **kwargs):
"""Log into the internal werkzeug logger."""
global _logger
if _logger is None:
import logging
_logger = logging.getLogger('werkzeug')
# Only set up a default log handler if the
# end-user application didn't set anything up.
if not logging.root.handlers and _logger.level == logging.NOTSET:
_logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
_logger.addHandler(handler)
getattr(_logger, type)(message.rstrip(), *args, **kwargs)
如果想要修改打印日志,可以在启动Flask之前,定义一个root logger
import logging
logger