'logger_name' 使用 Flask 在我的 python 应用中启用 cors 时丢失
'logger_name' missing when enabling cors in my python app using flask
我正在尝试让 CORS 正常工作。一种解决方法有效,但不适用于所有 api。所以我希望有正常的工作方式。现在我已经在这里呆了一段时间了。我尝试像这样初始化 CORS:
flask_app = Flask(__name__)
CORS(flask_app)
这给了我以下错误:
return logging.getLogger("%s.cors" % app.logger_name) AttributeError: 'Flask' object has no attribute 'logger_name'
完整的错误是:
2021-12-18 16:30:57 default[20211218t172924] Traceback (most recent call last):
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker worker.init_process()
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/workers/base.py", line 134, in init_process self.load_wsgi()
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi self.wsgi = self.app.wsgi()
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/app/base.py", line 67, in wsgi self.callable = self.load()
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 58, in load return self.load_wsgiapp()
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp return util.import_app(self.app_uri)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/util.py", line 412, in import_app app = app(*args, **kwargs)
File "/workspace/app.py", line 38, in get_flask_app CORS(flask_app)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask_cors/extension.py", line 59, in __init__ self.init_app(app, **kwargs)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask_cors/extension.py", line 81, in init_app getLogger(app).info("Configuring CORS with resources: %s", resources_human)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask_cors/core.py", line 356, in getLogger
return logging.getLogger("%s.cors" % app.logger_name) AttributeError: 'Flask' object has no attribute 'logger_name'
当我删除 CORS(flask_app) 时,一切正常。除了我想修复的 CORS 问题。
这是我的完整app.py:
# flask packages
from flask import Flask, request, app
from flask_cors import CORS
from flask_restful import Api
from flask_mongoengine import MongoEngine
from flask_jwt_extended import JWTManager
import logging
# local packages
from api.routes import create_routes
# external packages
import os
# default mongodb configuration
default_config = {'MONGODB_SETTINGS': {
'db': '(....)',
'host': '(....)',
'port': 27017,
'username': 'admin',
'password': 'password',
'authentication_source': 'admin'},
'JWT_SECRET_KEY': '(...)'}
def get_flask_app(config: dict = None) -> app.Flask:
"""
Initializes Flask app with given configuration.
Main entry point for wsgi (gunicorn) server.
:param config: Configuration dictionary
:return: app
"""
# init flask
logging.getLogger('flask_cors').level = logging.DEBUG
flask_app = Flask(__name__)
CORS(flask_app)
# configure app
config = default_config if config is None else config
flask_app.config.update(config)
# load config variables
if 'MONGODB_URI' in os.environ:
flask_app.config['MONGODB_SETTINGS'] = {'host': os.environ['MONGODB_URI'],
'retryWrites': False}
if 'JWT_SECRET_KEY' in os.environ:
flask_app.config['JWT_SECRET_KEY'] = os.environ['JWT_SECRET_KEY']
# init api and routes
api = Api(app=flask_app)
create_routes(api=api)
# init mongoengine
db = MongoEngine(app=flask_app)
# init jwt manager
jwt = JWTManager(app=flask_app)
return flask_app
if __name__ == '__main__':
# Main entry point when run in stand-alone mode.
app = get_flask_app()
app.run(host='0.0.0.0', port=8080)
使用Google云平台。这可能是我的问题的关键吗?我的 app.yaml 是:
runtime: python38
entrypoint: gunicorn -b :$PORT 'app:get_flask_app()'
我会尝试看看本地发生了什么,也许在这里更容易解决。想着什么时候在这里重新输入问题。
已经更新了我的 Flask-Cors (flask-cors)。试了很多东西都没用,所以才有这个问题。
非常感谢/谢谢!!
添加这部分:
Flask.logger_name = "listlogger"
终于帮我解决了。现在它能够找到 logger_name。该列表记录器只是一个随机名称。
我在打开应用程序之前添加了它 + CORS(app):
Flask.logger_name = "listlogger"
flask_app = Flask(__name__)
CORS(flask_app)
我正在尝试让 CORS 正常工作。一种解决方法有效,但不适用于所有 api。所以我希望有正常的工作方式。现在我已经在这里呆了一段时间了。我尝试像这样初始化 CORS:
flask_app = Flask(__name__)
CORS(flask_app)
这给了我以下错误:
return logging.getLogger("%s.cors" % app.logger_name) AttributeError: 'Flask' object has no attribute 'logger_name'
完整的错误是:
2021-12-18 16:30:57 default[20211218t172924] Traceback (most recent call last):
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker worker.init_process()
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/workers/base.py", line 134, in init_process self.load_wsgi()
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi self.wsgi = self.app.wsgi()
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/app/base.py", line 67, in wsgi self.callable = self.load()
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 58, in load return self.load_wsgiapp()
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp return util.import_app(self.app_uri)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/util.py", line 412, in import_app app = app(*args, **kwargs)
File "/workspace/app.py", line 38, in get_flask_app CORS(flask_app)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask_cors/extension.py", line 59, in __init__ self.init_app(app, **kwargs)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask_cors/extension.py", line 81, in init_app getLogger(app).info("Configuring CORS with resources: %s", resources_human)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask_cors/core.py", line 356, in getLogger
return logging.getLogger("%s.cors" % app.logger_name) AttributeError: 'Flask' object has no attribute 'logger_name'
当我删除 CORS(flask_app) 时,一切正常。除了我想修复的 CORS 问题。
这是我的完整app.py:
# flask packages
from flask import Flask, request, app
from flask_cors import CORS
from flask_restful import Api
from flask_mongoengine import MongoEngine
from flask_jwt_extended import JWTManager
import logging
# local packages
from api.routes import create_routes
# external packages
import os
# default mongodb configuration
default_config = {'MONGODB_SETTINGS': {
'db': '(....)',
'host': '(....)',
'port': 27017,
'username': 'admin',
'password': 'password',
'authentication_source': 'admin'},
'JWT_SECRET_KEY': '(...)'}
def get_flask_app(config: dict = None) -> app.Flask:
"""
Initializes Flask app with given configuration.
Main entry point for wsgi (gunicorn) server.
:param config: Configuration dictionary
:return: app
"""
# init flask
logging.getLogger('flask_cors').level = logging.DEBUG
flask_app = Flask(__name__)
CORS(flask_app)
# configure app
config = default_config if config is None else config
flask_app.config.update(config)
# load config variables
if 'MONGODB_URI' in os.environ:
flask_app.config['MONGODB_SETTINGS'] = {'host': os.environ['MONGODB_URI'],
'retryWrites': False}
if 'JWT_SECRET_KEY' in os.environ:
flask_app.config['JWT_SECRET_KEY'] = os.environ['JWT_SECRET_KEY']
# init api and routes
api = Api(app=flask_app)
create_routes(api=api)
# init mongoengine
db = MongoEngine(app=flask_app)
# init jwt manager
jwt = JWTManager(app=flask_app)
return flask_app
if __name__ == '__main__':
# Main entry point when run in stand-alone mode.
app = get_flask_app()
app.run(host='0.0.0.0', port=8080)
使用Google云平台。这可能是我的问题的关键吗?我的 app.yaml 是:
runtime: python38
entrypoint: gunicorn -b :$PORT 'app:get_flask_app()'
我会尝试看看本地发生了什么,也许在这里更容易解决。想着什么时候在这里重新输入问题。
已经更新了我的 Flask-Cors (flask-cors)。试了很多东西都没用,所以才有这个问题。
非常感谢/谢谢!!
添加这部分:
Flask.logger_name = "listlogger"
终于帮我解决了。现在它能够找到 logger_name。该列表记录器只是一个随机名称。
我在打开应用程序之前添加了它 + CORS(app):
Flask.logger_name = "listlogger"
flask_app = Flask(__name__)
CORS(flask_app)