为什么不这样做呢?
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/



