在烧瓶中使用带有应用程序上下文的全局变量



我在mylogger.py中定义了一个自定义记录器,日志目录被定义为config.py为log_dir='/var/log'。在mylogger.py中,记录器使用LOG_DIR进行一些初始化工作。

//mylogger.py
from flask import current_app
log_path = os.path.join(current_app.config['LOG_DIR'],"all.log") 
handler = logging.FileHandler(filename = logPath)
....
//config.py
LOG_DIR = "/var/log"

Flask说一条错误信息:

This typically means that you attempted to use functionality that needed
to interface with the current application object in some way. To solve
this, set up an application context with modules.app_context().  See the
documentation for more information.

如何在app_context中使用config.py中的变量?非常感谢。

def register_logging(app):
log_path = os.path.join(app.config['LOG_DIR'],"all.log") 
handler = logging.FileHandler(filename = logPath)
def create_app():
app = Flask()
register_logging(app)
return app
app = create_app()

或者只导入config.py

from config import LOG_DIR

最新更新