为什么flask只在app.debug = True时记录?

时间:2013-12-06 10:22:37

标签: python flask

在main中:

handler = RotatingFileHandler('/tmp/mylog')
handler.setLevel(logging.DEBUG)
app.logger.addHandler(handler)
my_glob.logger = app.logger
app.debug = True
app.run(host='0.0.0.0', port=80)

在'url'中:

import my_glob
...
handling get request here:
  logger = my_glob.logger
  logger.info('this wont show unless app.debug=True is specified')
  logger.error('this always shows up')

如果我这样做,它就有效。如果我删除app.debug = True,它就不起作用。但是,flask文档说app.debug是在本地环境/调试,而不是生产。那么我应该使用什么来启用信息/调试级别的登录?

1 个答案:

答案 0 :(得分:4)

这是因为如果您没有设置日志级别,flask会将ERROR视为默认日志记录级别,请参阅:https://github.com/mitsuhiko/flask/blob/master/flask/logging.py#L28

解决方案是在使用前添加app.logger.setLevel(logging.DEBUG)