flask 如何把日志输出到日志文件中

关于日志这块,我看很多是拿原生的logging模块来打印,但是flask的日志,输入到文件里?
该怎么操作?


把flask输出的日志,输出到文件里

log = LogUtils()

def create_app(NEVNAME=None):
    if NEVNAME == None:
        NEVNAME = os.getenv("FLASK_ENV") or "test"
    # 创建Flask对象

    app = APIFlask(__name__)
    # 加载配置信息
    config = APPCONFIG[NEVNAME]
   #获取日志
    app.logger.addHandler(log.handler)
    app.config.from_object(config)
    # 懒加载,注册插件
    init_plugs(app)
    # 注册蓝图
    init_api(app)
    return app

LogUtils文件

@Singleton
class LogUtils(object):
    handler = None

    def __init__(self, name="app"):  # Logger标识默认为app
        """
        :param name: 业务名称
        :param filename: 文件名称
        """
        self.handler = logbook.FileHandler(LOG_PATH, encoding='utf-8', format_string=u'''\
Subject: Application Error for {record.extra[path]} [{record.extra[method]}]

Message type:       {record.level_name}
Location:           {record.filename}:{record.lineno}
Module:             {record.module}
Function:           {record.func_name}
Time:               {record.time:%Y-%m-%d %H:%M:%S}
Remote IP:          {record.extra[ip]}
Request:            {record.extra[path]} [{record.extra[method]}]

Message:

{record.message}
''')

        self.handler.push_application()
        logbook.set_datetime_format("local")  # 将日志时间设置为本地时间

        # # 控制台输出
        # self.handler = ColorizedStderrHandler(bubble=True)
        # self.handler.push_application()

        self.logger = logbook.Logger(name)

    def info(self, *args, **kwargs):
        return self.logger.info(*args, **kwargs)

    def error(self, *args, **kwargs):
        return self.logger.error(*args, **kwargs)

    def warning(self, *args, **kwargs):
        return self.logger.warning(*args, **kwargs)

    def debug(self, *args, **kwargs):
        return self.logger.debug(*args, **kwargs)

配置一个文件相关的日志处理器,比如 FileHandler,然后使用 app.logger.addHandler 方法注册:

app.logger.addHandler(file_handler)