栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

flask日志记录-无法将其写入文件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

flask日志记录-无法将其写入文件

为什么不这样做呢?

if __name__ == '__main__':    init_db()  # or whatever you need to do    import logging    logging.basicConfig(filename='error.log',level=logging.DEBUG)    app.run(host="0.0.0.0")

如果现在启动应用程序,你将看到error.log包含:

INFO:werkzeug: * Running on http://0.0.0.0:5000/

有关更多信息,请访问http://docs.python.org/2/howto/logging.html

好的,因为你坚持使用我展示的方法不能拥有两个处理程序,所以我将添加一个示例,使这一点很清楚。首先,将此日志代码添加到你的主目录中:

import logging, logging.config, yamllogging.config.dictConfig(yaml.load(open('logging.conf')))

现在还添加一些调试代码,以便我们可以看到我们的设置可以正常工作:

logfile    = logging.getLogger('file')logconsole = logging.getLogger('console')logfile.debug("Debug FILE")logconsole.debug("Debug CONSOLE")

剩下的就是“ logging.conf”程序。让我们使用它:

version: 1formatters:  hiformat:    format: 'HI %(asctime)s - %(name)s - %(levelname)s - %(message)s'  simple:    format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'handlers:  console:    class: logging.StreamHandler    level: DEBUG    formatter: hiformat    stream: ext://sys.stdout  file:    class: logging.FileHandler    level: DEBUG    formatter: simple    filename: errors.logloggers:  console:    level: DEBUG    handlers: [console]    propagate: no  file:    level: DEBUG    handlers: [file]    propagate: noroot:  level: DEBUG  handlers: [console,file]

此配置比所需的配置复杂得多,但它还显示了日志记录模块的某些功能。

现在,当我们运行我们的应用程序时,我们看到以下输出(werkzeug和console-logger):

HI 2013-07-22 16:36:13,475 - console - DEBUG - Debug ConSOLEHI 2013-07-22 16:36:13,477 - werkzeug - INFO -  * Running on http://0.0.0.0:5000/

另请注意,使用了带有“ HI”的自定义格式器。

现在查看“ errors.log”文件。它包含了:

2013-07-22 16:36:13,475 - file - DEBUG - Debug FILE2013-07-22 16:36:13,477 - werkzeug - INFO -  * Running on http://0.0.0.0:5000/


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/402478.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号