def emit(self, record): """Emit a record. Append the record to the buffer after it has been transformed by process_record(). If the length of the buffer is greater than or equal to its capacity, then the flush() event is rescheduled to immediately process the buffer. """ processed_record = self.process_record(record) with self.__emit_lock: self.__emit_buffer.append(processed_record) if self.__flush_event is None: # Now that we've added our first record to the buffer, we schedule a call to flush() # to occur 'self.interval_secs' seconds from now. 'self.__flush_event' should never # be None after this point. self.__flush_event = flush.flush_after( self, delay=self.interval_secs) if not self.__flush_scheduled_by_emit and len( self.__emit_buffer) >= self.capacity: # Attempt to flush the buffer early if we haven't already done so. We don't bother # calling flush.cancel() and flush.flush_after() when 'self.__flush_event' is # already scheduled to happen as soon as possible to avoid introducing unnecessary # delays in emit(). if flush.cancel(self.__flush_event): self.__flush_event = flush.flush_after(self, delay=0.0) self.__flush_scheduled_by_emit = True
def flush(self): """Ensure all logging output has been flushed.""" self.__flush(close_called=False) with self.__emit_lock: if self.__flush_event is not None and not self.__close_called: # We cancel 'self.__flush_event' in case flush() was called by someone other than # the flush thread to avoid having multiple flush() events scheduled. flush.cancel(self.__flush_event) self.__flush_event = flush.flush_after(self, delay=self.interval_secs) self.__flush_scheduled_by_emit = False