def test_must_map_formatters_sequentially(self, pretty_print_mock): events_iterable = [1, 2, 3] expected_result = [1, 2, 3] expected_call_order = [ call(1, colored=self.colored_mock), call(2, colored=self.colored_mock), call(3, colored=self.colored_mock), ] formatter = LogsFormatter(self.colored_mock, self.formatter_chain) result_iterable = formatter.do_format(events_iterable) self.assertEqual(list(result_iterable), expected_result) self.chain_method1.assert_has_calls(expected_call_order) self.chain_method2.assert_has_calls(expected_call_order) self.chain_method3.assert_has_calls(expected_call_order) pretty_print_mock.assert_has_calls(expected_call_order) # Pretty Printer must always be called
def test_must_work_without_formatter_chain(self, pretty_print_mock): events_iterable = [1, 2, 3] expected_result = [1, 2, 3] expected_call_order = [ call(1, colored=self.colored_mock), call(2, colored=self.colored_mock), call(3, colored=self.colored_mock), ] # No formatter chain. formatter = LogsFormatter(self.colored_mock) result_iterable = formatter.do_format(events_iterable) self.assertEqual(list(result_iterable), expected_result) # Pretty Print is always called, even if there are no other formatters in the chain. pretty_print_mock.assert_has_calls(expected_call_order) self.chain_method1.assert_not_called() self.chain_method2.assert_not_called() self.chain_method3.assert_not_called()
def test_must_serialize_event(self): colored_timestamp = "colored timestamp" colored_stream_name = "colored stream name" self.colored_mock.yellow.return_value = colored_timestamp self.colored_mock.cyan.return_value = colored_stream_name event = LogEvent(self.group_name, self.event_dict) expected = " ".join([colored_stream_name, colored_timestamp, self.message]) result = LogsFormatter._pretty_print_event(event, self.colored_mock) self.assertEqual(expected, result) self.colored_mock.yellow.has_calls() self.colored_mock.cyan.assert_called_with(self.stream_name)
def formatter(self): """ Creates and returns a Formatter capable of nicely formatting Lambda function logs Returns ------- LogsFormatter """ formatter_chain = [ LambdaLogMsgFormatters.colorize_errors, # Format JSON "before" highlighting the keywords. Otherwise, JSON will be invalid from all the # ANSI color codes and fail to pretty print JSONMsgFormatter.format_json, KeywordHighlighter(self._filter_pattern).highlight_keywords, ] return LogsFormatter(self.colored, formatter_chain)