2017-03-08 3 views
0

Ich habe ein Problem mit einem Protokolldatei Schreibstrom durch pyinotify und es ist Threads. Ich verwende , um ein Verzeichnis für CLOSE_WRITE Dateiereignisse zu überwachen. Bevor ich pyinotify initialisieren erstelle ich einen Protokolldatenstrom wie der eingebaute in logging Modul so:Python Persist Protokolldatei-Stream mit 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') 

Also, nach meiner __log Variable es sofort funktioniert initialisiert erhalten, so dass für Protokoll schreibt. Ich möchte als nächstes die pyinotify Instanz starten und möchte __log mit den folgenden Klassendefinitionen weitergeben müssen:

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 

In der obigen Ausführung, mein process_IN_CLOSE_WRITE Methode genau aus dem pyinotify.AsyncNotifier wie erwartet ausgelöst wird; Die Protokollzeile für logging some cool stuff schreibt jedoch nie in die Protokolldatei.

Ich habe das Gefühl, dass es etwas damit zu tun hat, den Dateistrom durch den pyinotify Threading-Prozess zu erhalten; Ich bin mir jedoch nicht sicher, wie ich das beheben soll.

Irgendwelche Ideen?

Antwort

0

Ich könnte eine Lösung gefunden haben, die zu funktionieren scheint. Ich bin mir nicht sicher, ob dies der beste Ansatz ist, deshalb werde ich das OP für den Moment offen lassen, um zu sehen, ob irgendwelche anderen Ideen gepostet werden.

Ich denke, ich habe meine pyinotify.AsyncNotifier Setup falsch behandelt. Ich habe die Umsetzung:

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

Nun, wenn meine Wrapper-Klasse processEvents auf Instanziierung des Hörers ausgelöst wird, und wenn ein Ereignis von einem CLOSE_WRITE Ereignis ausgelöst wird, wird das log Objekt gepflegt und angemessen übergeben und empfangen können Ereignisse schreiben

+0

Leider, sobald ich die Komponente der Dämonisierung mit 'Python-Daemon' und' files_preserve' hinzufügen, geht mein Protokolldatenstrom wieder weg und mein pynotify-Ereignis kann sich nicht einloggen – sadmicrowave