from flask import Flask
import logging
app = Flask(__name__)
@app.route('/')
def hello_world():
app.logger.error("hello")
return 'Hello World!'
if __name__ == '__main__':
handler = logging.FileHandler('flask.log', encoding='UTF-8')
handler.setLevel(logging.DEBUG)
logging_format = logging.Formatter(
'%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(lineno)s - %(message)s')
handler.setFormatter(logging_format)
app.logger.addHandler(handler)
app.run()
app.logger.error("????")
启动后访问 127.0.0.1:5000/
app.logger.error("hello")这个日志打印了,但是没有按设定的格式,而且也找不到 flask.log 文件
1
zeyexe 2021-06-12 15:01:37 +08:00
When you want to configure logging for your project, you should do it as soon as possible when the program starts.
https://flask.palletsprojects.com/en/2.0.x/logging/#basic-configuration 请尝试在 app = Flask(__name__) 之前配置日志。 |
2
johnsona 2021-06-12 17:55:36 +08:00 via iPhone
flask 有个 logger 属性 实际上是被 property 装饰的方法 创建对象的时候执行了 文档的建议是 你晚了就 remove 好像是的
|