主要内容:
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)
在“网址”中:
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 是在本地环境/调试中,而不是在生产中。那么我应该使用什么来启用信息/调试级别的日志记录?
最佳答案
这是因为如果您不设置日志级别,flask 会将 ERROR
视为默认日志级别,请参阅:https://github.com/mitsuhiko/flask/blob/master/flask/logging.py#L28 .
解决方案是在使用前添加app.logger.setLevel(logging.DEBUG)
。
关于python - 为什么 flask 只在 app.debug = True 时记录日志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20421470/