1import logging.handlers 2 3class TestHandler(logging.handlers.BufferingHandler): 4 def __init__(self, matcher): 5 # BufferingHandler takes a "capacity" argument 6 # so as to know when to flush. As we're overriding 7 # shouldFlush anyway, we can set a capacity of zero. 8 # You can call flush() manually to clear out the 9 # buffer. 10 logging.handlers.BufferingHandler.__init__(self, 0) 11 self.matcher = matcher 12 13 def shouldFlush(self): 14 return False 15 16 def emit(self, record): 17 self.format(record) 18 self.buffer.append(record.__dict__) 19 20 def matches(self, **kwargs): 21 """ 22 Look for a saved dict whose keys/values match the supplied arguments. 23 """ 24 result = False 25 for d in self.buffer: 26 if self.matcher.matches(d, **kwargs): 27 result = True 28 break 29 return result 30