def test_emit_does_not_drop_records_if_configured(self, MockWorker): buffer_capacity = 1 handler = TimberHandler(api_key=self.api_key, source_id=self.source_id, buffer_capacity=buffer_capacity, drop_extra_events=False) def consumer(q): while True: if q.full(): while not q.empty(): _ = q.get(block=True) time.sleep(.2) t = threading.Thread(target=consumer, args=(handler.pipe, )) t.daemon = True logger = logging.getLogger(__name__) logger.handlers = [] logger.addHandler(handler) logger.critical('hello') self.assertTrue(handler.pipe.full()) t.start() logger.critical('goodbye') logger.critical('goodbye2') self.assertEqual(handler.dropcount, 0)
def test_handler_creates_pipe_from_args(self, MockWorker): buffer_capacity = 9 flush_interval = 1 handler = TimberHandler(api_key=self.api_key, source_id=self.source_id, buffer_capacity=buffer_capacity, flush_interval=flush_interval) self.assertEqual(handler.pipe._maxsize, buffer_capacity)
def test_error_suppression(self, MockWorker): buffer_capacity = 1 handler = TimberHandler(api_key=self.api_key, source_id=self.source_id, buffer_capacity=buffer_capacity, raise_exceptions=True) handler.pipe = mock.MagicMock(put=mock.Mock(side_effect=ValueError)) logger = logging.getLogger(__name__) logger.handlers = [] logger.addHandler(handler) with self.assertRaises(ValueError): logger.critical('hello') handler.raise_exceptions = False logger.critical('hello')
def test_handler_creates_and_starts_worker_from_args(self, MockWorker): buffer_capacity = 9 flush_interval = 9 handler = TimberHandler(api_key=self.api_key, source_id=self.source_id, buffer_capacity=buffer_capacity, flush_interval=flush_interval) MockWorker.assert_called_with(handler.uploader, handler.pipe, buffer_capacity, flush_interval) self.assertTrue(handler.flush_thread.start.called)
def test_emit_starts_thread_if_not_alive(self, MockWorker): handler = TimberHandler(api_key=self.api_key, source_id=self.source_id) self.assertTrue(handler.flush_thread.start.call_count, 1) handler.flush_thread.is_alive = mock.Mock(return_value=False) logger = logging.getLogger(__name__) logger.handlers = [] logger.addHandler(handler) logger.critical('hello') self.assertEqual(handler.flush_thread.start.call_count, 2)
def test_create_frame_with_extra(self): handler = TimberHandler(api_key="some-api-key", source_id="some-source-id") log_record = logging.LogRecord("timber-test", 20, "/some/path", 10, "Some log message", [], None) extra = {'non_dict_key': 'string_value', 'dict_key': {'name': 'Test Test'}} log_record.__dict__.update(extra) # This is how `extra` gets included in the LogRecord # By default, non-dict keys are excluded. frame = create_frame(log_record, log_record.getMessage(), TimberContext()) self.assertTrue(frame['level'] == 'info') self.assertIn('dict_key', frame) self.assertNotIn('non_dict_key', frame) frame = create_frame(log_record, log_record.getMessage(), TimberContext(), include_all_extra=True) self.assertIn('non_dict_key', frame)
def test_emit_drops_records_if_configured(self, MockWorker): buffer_capacity = 1 handler = TimberHandler(api_key=self.api_key, source_id=self.source_id, buffer_capacity=buffer_capacity, drop_extra_events=True) logger = logging.getLogger(__name__) logger.handlers = [] logger.addHandler(handler) logger.critical('hello') logger.critical('goodbye') log_entry = handler.pipe.get() self.assertEqual(log_entry['message'], 'hello') self.assertTrue(handler.pipe.empty()) self.assertEqual(handler.dropcount, 1)
def test_handler_creates_uploader_from_args(self, MockWorker): handler = TimberHandler(api_key=self.api_key, source_id=self.source_id, host=self.host) self.assertEqual(handler.uploader.api_key, self.api_key) self.assertEqual(handler.uploader.host, self.host)
def test_create_log_entry(self): handler = TimberHandler(api_key="some-api-key", source_id="some-source-id") log_record = logging.LogRecord("timber-test", 20, "/some/path", 10, "Some log message", [], None) log_entry = create_log_entry(handler, log_record) self.assertTrue(log_entry['level'] == 'info')
def test_create_frame_happy_path(self): handler = TimberHandler(api_key="some-api-key", source_id="some-source-id") log_record = logging.LogRecord("timber-test", 20, "/some/path", 10, "Some log message", [], None) frame = create_frame(log_record, log_record.getMessage(), TimberContext()) self.assertTrue(frame['level'] == 'info')