title: Implementing a tally with python logger module
body: <p>I'm trying to create a python logger handler that handles tallies nicely.  For example, I might logging.debug something a thousand times a second.  After, say, 10 seconds, I want to logging.info the fact that that thing happened 10,000 times in those 10 seconds.  I have something set up to do it, but it seems kind of icky, and I was wondering if any python logging gurus could help out.  Ideally, I would like to have my tally handler periodically pass a tally record to the standard handler...</p>
  
  <p>Here's what I have so far:</p>
  
  <pre><code>class CounterHandler(logging.Handler):
    '''
    This handler is to log only counts of messages.  
    So, for instance, suppose we want to logging.debug event A 
    which happens about once a millisecond, but we may want to 
    logging.info every second how many times event A happened 
    during that second.
    '''
    def __init__(self, pattern, message, logger, howoften = 1, timer = time.time, leastlevel = logging.NOTSET, messagelevel = logging.INFO, plot = False):
      '''
      pattern: the regex pattern in log messages to count
      leastlevel: the least logging level to count
      handler: the handler to hand accumulated counts to
      message: the message to log when we log accumulated counts
      messagelevel: the logging level at which to log accumulated counts
      howoften: how often to log accumulated counts
      timer: the timer to use (default is real time, but you can use, e.g. simpy time or something else)
      '''
      logging.Handler.__init__(self)
      self._regex = re.compile(pattern)
      self._msg = message
      self._messagelevel = messagelevel
      self._leastlevel = level
      self._logger = logger
      self._count = 0
      self._howoften = howoften
      self._timer = timer
  
      self.stopper = threading.Event()
      self._propogation_thread = threading.Thread(target=self._propogate, args=(self.stopper, timer))
      self._propogation_thread.daemon = True
      self._propogation_thread.start()
  
    def _dump(self):
      self._logger.log(self._messagelevel, 
                       "{} happened {} times by time {}".format(self._msg, 
                                                                self._count, 
                                                                self._timer()))
  
  
    def _propogate(self, stopper, timer):
      '''
      This is a thread that dumps the counts every so often 
      '''
      lasttime = timer()
      while not stopper.is_set():
        if lasttime + self._howoften &lt; timer():
          self._dump()
  
    def emit(self, record):
      if record.level &gt;= self._level and regex.match(record.message):
        self._count += 1
  </code></pre>
  
  <p>EDIT:
  @Reut, The other side of this problem is actually complicated.  The reason I want to hand it to another handler is because I want to have flexibility in what I do with the counts that I get.  I have a module that graphs counts in real time, but I also have a log file that it would be nice to output to.  In which case, it seems that I should be doing something along the lines of chaining handlers, then I can add or subtract functionality as I see fit.  Viz: </p>
  
  <pre><code>The big picture:
                                                 ,-&gt; LogFileHandler    
  My Code -&gt; Log Message -&gt; Logger -&gt; LogRecord &lt;                    ,-&gt; GrapherHandler
                                                 `-&gt; CounterHandler &lt;
                                                                     `-&gt; LogFileHandler
  </code></pre>