Commits

Christopher Nilsson committed 9569ab0

added flag to prevent re-adding log handlers for multiple instances of class.

eg. ugly with unittests

  • Participants
  • Parent commits a5fcce9

Comments (0)

Files changed (2)

 
 LOG_NAME = "dirwatch"
 LOG_STANDARD_FMT = "%(asctime)s p=%(process)d t=%(thread)d [%(levelname)s] %(message)s"
+_LOG_HANDLER_ADDED = False
 
 class DirWatcher(object):
     def __init__(self):
         self.log = logging.getLogger(LOG_NAME)
         self.log.setLevel(verbosity)
         
-        # log format
-        fmt = logging.Formatter(LOG_STANDARD_FMT)
-        
-        # console output
-        console = logging.StreamHandler()
-        console.setLevel(verbosity)
-        console.setFormatter(fmt)
-        
-        self.log.addHandler(console)
+        global _LOG_HANDLER_ADDED
+        if not _LOG_HANDLER_ADDED:
+            # log format
+            fmt = logging.Formatter(LOG_STANDARD_FMT)
+            
+            # console output
+            console = logging.StreamHandler()
+            #console.setLevel(verbosity)
+            console.setFormatter(fmt)
+            
+            self.log.addHandler(console)
+            _LOG_HANDLER_ADDED = True
         
     def _process_incoming_queue(self):
         while self.monitoring:

dirwatch/nt_dirwatch.py

         self._iocp = None
         
         # Add some persistent NTEventLog logging for warnings & errors.
-        try:
-            nteventlog_handler = logging.handlers.NTEventLogHandler("dirwatch")
-            nteventlog_handler.setFormatter(logging.Formatter(LOG_STANDARD_FMT))
-            nteventlog_handler.setLevel(logging.WARNING)
-            self.log.addHandler(nteventlog_handler)
-        except Exception, err:
-            self.log.error("Failed to add NTEventLog handler: %s", err)
+        #try:
+        #    nteventlog_handler = logging.handlers.NTEventLogHandler("dirwatch")
+        #    nteventlog_handler.setFormatter(logging.Formatter(LOG_STANDARD_FMT))
+        #    nteventlog_handler.setLevel(logging.WARNING)
+        #    self.log.addHandler(nteventlog_handler)
+        #except Exception, err:
+        #    self.log.error("Failed to add NTEventLog handler: %s", err)
         
     def start_monitor(self):