python - 为什么 flask 只在 app.debug = True 时记录日志?

标签 python flask

主要内容:

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/

相关文章:

python - Django表单验证概述(快速!)

python - 停止 Pygame 中的多个键盘输入

python - sqlalchemy.exc.OperationalError : (psycopg2. OperationalError) FATAL: 数据库不存在

python - 为什么运行 Flask 开发服务器会自行运行两次?

python - python 记录器中的序言和尾声

python - 获取具有两个特定关键字的行

python - 当我们使用 python-netaddr 拥有子网和 IPv4 地址时,如何获取 IP 前缀?

amazon-web-services - 使用 `eb local run`运行应用程序有效,但实际上无法连接到应用程序

javascript - 如何在 JS 代码中使用 Flask.Response?

Flask Restx 枚举模型