Пример #1
0
    def __init__(self):
        self.event_logger = logging.getLogger()
        # We log events to the root logger, which is backed by a
        # "StartupHandler" log handler.  The "StartupHandler" buffers
        # log messages.  When the "real" loggers are set up, we flush
        # accumulated messages in StartupHandler's buffers to the real
        # logger.
        formatter = logging.Formatter(
            "%(asctime)s %(levelname)s %(name)s %(message)s",
            "%Y-%m-%d %H:%M:%S")
        self.debug_handler = loghandler.StreamHandler()
        self.debug_handler.setFormatter(formatter)
        self.debug_handler.setLevel(logging.WARN)

        self.startup_handler = loghandler.StartupHandler()

        self.event_logger.addHandler(self.debug_handler)
        self.event_logger.addHandler(self.startup_handler)
Пример #2
0
    def test_buffer(self):
        handler = loghandler.StartupHandler()
        self.assertFalse(handler.shouldFlush(None))
        self.assertEqual(maxsize, handler.capacity)

        records = []

        def handle(record):
            records.append(record)

        handle.handle = handle

        handler.flushBufferTo(handle)
        self.assertEqual([], records)

        handler.buffer.append(1)
        handler.flushBufferTo(handle)
        self.assertEqual([1], records)

        del handle.handle