如何从uswgi+flask应用程序记录错误



我是wsgi和flask的新手,想检查日志。但是,我无法将日志打印到文件中。我用这个教程来设置我的项目。我想记录一些事情,比如python中的打印语句和occe中的错误。

这是我对usgwi server.ini的配置:

[uwsgi]
module = wsgi:app
master = false
processes = 1
socket = myproject.sock
socket = stats.sock
chmod-socket = 660
vacuum = true
die-on-term = true
plugins-dir = /usr/lib/uwsgi/plugins
plugins = logfile

logger = file:logs.log
logger-req = file:requests.log
stats = myproject.sock

logs.log和requests.log永远不会被填充(master=false,process=1,因为我在应用程序中使用tensorflow(

然后我有wsgi.py

from main import app
if __name__ == "__main__":
app.run()

然后烧瓶应用程序main.py

app = Flask(__name__, static_url_path='/www')
@app.route('/')
def send_index():
print("I want to print this to logs")
x = 1 / 0 # I would like this to throw a ZeroDivisionError and send it to logs
return send_from_directory('www', 'index.html')
if __name__ == '__main__':
app.run(host='0.0.0.0')

一种方法是使用"logging"包在应用程序中使用日志记录。一些快速提示:

logging.basicConfig(filename='record.log', level=logging.DEBUG, format=f'%(asctime)s %(levelname)s %(name)s %(threadName)s : %(message)s')

应该在应用程序声明后在main.py中调用。您也可以使用:

current_app.logger.info("Your message here")

以记录特定事件。

最新更新