2017-03-08 72 views
0

我遇到了一個問題,通過pyinotify及其線程持續存在日誌文件寫入流。我正在使用pyinotify來監視CLOSE_WRITE文件事件的目錄。之前我初始化pyinotify我使用內置的logging模塊,像這樣創建一個日誌流:Python持久化PyInotify日誌文件流

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') 

於是,經過我的__log變量初始化得到它的工作原理,立即允許日誌寫入。我想下次啓動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 

在上面的實現,我process_IN_CLOSE_WRITE方法被觸發正是從pyinotify.AsyncNotifier預期;但是,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'添加了demonization組件,我的日誌流再次消失,我的pyinotify事件無法登錄到它 – sadmicrowave