2017-03-08 5 views
0

pyinotifyのログファイル書き込みストリームとそのスレッドを永続化する際に問題が発生します。 CLOSE_WRITEファイルイベントのディレクトリを監視するのに、pyinotifyを使用しています。私はpyinotifyを初期化する前に、私はビルトインloggingモジュールそうのような使用してログ・ストリームを作成します。私の__log変数が初期化されます後PythonでPythonの永続化ログファイルストリーム

import os, logging 
from logging import handlers 
from logging.config import dictConfig 


log_dir = './var/log' 
name = 'com.sadmicrowave.tesseract' 
LOG_SETTINGS = { 'version' : 1 
       ,'handlers': { 'core': { 
            # make the logger a rotating file handler so the file automatically gets archived and a new one gets created, preventing files from becoming too large they are unmaintainable. 
            'class'  : 'logging.handlers.RotatingFileHandler' 
            # by setting our logger to the DEBUG level (lowest level) we will include all other levels by default 
            ,'level'  : 'DEBUG' 
            # this references the 'core' handler located in the 'formatters' dict element below 
            ,'formatter' : 'core' 
            # the path and file name of the output log file 
            ,'filename'  : os.path.join(log_dir, "%s.log" % name) 
            ,'mode'   : 'a' 
            # the max size we want to log file to reach before it gets archived and a new file gets created 
            ,'maxBytes'  : 100000 
            # the max number of files we want to keep in archive 
            ,'backupCount' : 5 } 
          } 
          # create the formatters which are referenced in the handlers section above 
          ,'formatters': {'core': {'format': '%(levelname)s %(asctime)s %(module)s|%(funcName)s %(lineno)d: %(message)s' 
              } 
          } 
          ,'loggers' : {'root': { 
                 'level'  : 'DEBUG' # The most granular level of logging available in the log module 
                 ,'handlers' : ['core'] 
              } 
          } 
         } 

# use the built-in logger dict configuration tool to convert the dict to a logger config 
dictConfig(LOG_SETTINGS) 

# get the logger created in the config and named root in the 'loggers' section of the config 
__log = logging.getLogger('root') 

だから、それは、ログの書き込みを可能にし、すぐに動作します。私は次のpyinotifyインスタンスを開始したいと、次のクラス定義を使用して__logに合格したいと思います:

import asyncore, pyinotify 

class Notify (object): 
    def __init__ (self, log=None, verbose=True): 
     wm = pyinotify.WatchManager() 
     wm.add_watch('/path/to/folder/to/monitor/', pyinotify.IN_CLOSE_WRITE, proc_fun=processEvent(log, verbose)) 

     notifier = pyinotify.AsyncNotifier(wm, None) 
     asyncore.loop() 

class processEvent (pyinotify.ProcessEvent): 
    def __init__ (self, log=None, verbose=True): 
     log.info('logging some cool stuff') 

     self.__log    = log 
     self.__verbose   = verbose 

    def process_IN_CLOSE_WRITE (self, event): 
     print event 

上記の実装では、pyinotify.AsyncNotifierから予想されるように私のprocess_IN_CLOSE_WRITE方法が正確にトリガーされます。ただし、logging some cool stuffのログ行はログファイルに書き込まれません。

私は、それがpyinotifyスレッドプロセスを通じてファイルストリームを永続化することと関係があるように感じています。しかし、私はこれを解決する方法がわかりません。

アイデア?

答えて

0

解決策が見つかった可能性があります。それが最良のアプローチであるかどうかは分かりませんので、OPを開いたままにして、他のアイデアが投稿されているかどうかを確認します。

pyinotify.AsyncNotifierの設定が間違っていると思います。

class Notify (object): 
    def __init__ (self, log=None, verbose=True): 
     notifiers = [] 
     descriptors = [] 
     wm = pyinotify.WatchManager() 
     notifiers.append (pyinotify.AsyncNotifier(wm, processEvent(log, verbose))) 
     descriptors.append(wm.add_watch('/path/to/folder/to/monitor/', pyinotify.IN_CLOSE_WRITE, proc_fun=processEvent(log, verbose), auto_add=True) 

     asyncore.loop() 

さて、私のラッパークラスprocessEventsがリスナーのインスタンス化、およびイベントがCLOSE_WRITEイベントからトリガされたとき、logオブジェクトが維持されるでトリガし、適切に渡されるときに受け取ることができます:私はへの実装を変更しましたイベントを書き込みます。

+0

残念ながら、私は 'python-daemon'と' files_preserve'を使ってデーモン化の要素を追加すると、ログ・ストリームが再び消え、私のpyinotifyイベントはログに記録されません – sadmicrowave

関連する問題