Ma Flask应用程序在 Gunicorn 下工作,我正在尝试将所有日志输出到一个文件中。谷歌搜索后,我启用了日志记录,如下所示:
from werkzeug.exceptions import NotFound
from config import DEBUG, SERVER
from database import db_session
from flask import Flask, make_response
from flask_cors import CORS
import logging
import numpy as np
from route import MyApi
from utils import site_map
app = Flask(__name__)
CORS(app)
@app.teardown_appcontext
def shutdown_session(exception=None):
db_session.remove()
@app.errorhandler(NotFound)
def handle_not_found(e):
app.logger.debug('Test shutdown_session')
site_map(app)
return make_response({'error': e.description}, 404)
if __name__ == "__main__":
# Gunicorn logging
gunicorn_logger = logging.getLogger('gunicorn.error')
app.logger.handlers = gunicorn_logger.handlers
app.logger.setLevel(gunicorn_logger.level)
api = MyApi(app)
api.register()
app.config['SERVER_NAME'] = SERVER['host'] + ':' + str(SERVER['port'])
app.run(host=SERVER['host'], port=SERVER['port'], debug=DEBUG)
然后运行带有下一个参数的gunicorn:
gunicorn -b 0.0.0.0:5000 --log-file="log.txt" --log-level=DEBUG app:app
但是我只能看到Gunicorn的日志,尽管确实调用了404处理程序,app.logger.debug('Test shutdown_session')
却没有输出任何信息(我可以从Chrome中的该处理程序看到JSON请求无效的URL)。
问题出在哪里?
PS 。虚拟环境已激活。