2016-09-13 4 views
5

私はcurrent_app.loggerを使用しています。スレッド内にログインしようとすると「アプリケーションコンテキスト外で作業中」と表示されます。スレッド内で実行中のメソッドからメッセージを記録するにはどうすればよいですか?現在のモジュールのロガーを取得し、それを使用して、メッセージをログに記録:あなたが標準的な方法で標準loggingモジュールを使用フラスコcurrent_app.loggerをスレッド内で使用

def background(): 
    current_app.logger.debug('logged from thread') 

@app.route('/') 
def index(): 
    Thread(target=background).start() 
    return 'Hello, World!' 
Exception in thread Thread-16: 
Traceback (most recent call last): 
    File "/usr/lib64/python3.5/threading.py", line 914, in _bootstrap_inner 
    self.run() 
    File "/usr/lib64/python3.5/threading.py", line 862, in run 
    self._target(*self._args, **self._kwargs) 
    File "/home/sapam/demo.py", line 57, in background 
    current_app.logger.critical('test') 
    File "/home/sapam/.virtualenvs/demo/lib/python3.5/site-packages/werkzeug/local.py", line 343, in __getattr__ 
    return getattr(self._get_current_object(), name) 
    File "/home/sapam/.virtualenvs/demo/lib/python3.5/site-packages/werkzeug/local.py", line 302, in _get_current_object 
    return self.__local() 
    File "/home/sapam/.virtualenvs/demo/lib/python3.5/site-packages/flask/globals.py", line 51, in _find_app 
    raise RuntimeError(_app_ctx_err_msg) 
RuntimeError: Working outside of application context. 

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

127.0.0.1 - - [13/Sep/2016 12:28:24] "GET/HTTP/1.1" 200 - 

答えて

7

def background(): 
    logging.getLogger(__name__).debug('logged from thread') 

app.loggerは主に内部フラスコロギング、またはアプリケーション・コンテキスト内の少なくともロギングのために意味されます。あなたがスレッド内にいる場合、あなたはもはや同じアプリコンテキストにいません。

current_app._get_current_object()をスレッドに渡して、current_appの代わりにそのスレッドを使用できます。または、サブクラスThreadに類似したことをすることができます。

def background(app): 
    app.logger.debug('logged from thread') 

@app.route('/') 
def index(): 
    Thread(target=background, kwargs={'app': current_app._get_current_object()}).start() 
    return 'Hello, World!' 
class FlaskThread(Thread): 
    def __init__(self, *args, **kwargs): 
     super().__init__(*args, **kwargs) 
     self.app = current_app._get_current_object() 

    def run(self): 
     with self.app.app_context(): 
      super().run() 

def background(): 
    current_app.logger.debug('logged from thread') 

@app.route('/') 
def index(): 
    FlaskThread(target=background).start() 
    return 'Hello, World!' 
関連する問題