Использование глобальной переменной с контекстом приложения в flask - PullRequest
0 голосов
/ 05 августа 2020

Я определил настраиваемый регистратор в mylogger.py , а каталог журнала определен как config.py как LOG_DIR = '/ var / log'. В mylogger.py регистратор выполняет некоторую работу init с 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.

Как я могу использовать переменную в конфигурации .py в app_context? Спасибо.

1 Ответ

1 голос
/ 05 августа 2020
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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...