Exemplo n.º 1
0
 def test_format(self):
     """
     Test format method from JSONFormatter
     """
     log_record = makeLogRecord({"label": "value"})
     json_fmt = JSONFormatter(json_fields=["label"])
     self.assertEqual(json_fmt.format(log_record), '{"label": "value"}')
Exemplo n.º 2
0
 def test_format_with_extras(self):
     """
     Test format with extras method from JSONFormatter
     """
     log_record = makeLogRecord({"label": "value"})
     json_fmt = JSONFormatter(json_fields=["label"],
                              extras={'pod_extra': 'useful_message'})
     # compare as a dicts to not fail on sorting errors
     self.assertDictEqual(json.loads(json_fmt.format(log_record)), {
         "label": "value",
         "pod_extra": "useful_message"
     })
Exemplo n.º 3
0
    def set_context(self, ti: TaskInstance) -> None:
        """
        Provide task_instance context to airflow task handler.

        :param ti: task instance object
        """
        self.mark_end_on_close = not ti.raw

        if self.json_format:
            self.formatter = JSONFormatter(
                json_fields=self.json_fields,
                extras={
                    'dag_id': str(ti.dag_id),
                    'task_id': str(ti.task_id),
                    'execution_date':
                    self._clean_execution_date(ti.execution_date),
                    'try_number': str(ti.try_number),
                    'log_id': self._render_log_id(ti, ti.try_number),
                    'offset': int(time() * (10**9)),
                },
            )

        if self.write_stdout:
            if self.context_set:
                # We don't want to re-set up the handler if this logger has
                # already been initialized
                return

            self.handler = logging.StreamHandler(
                stream=sys.__stdout__)  # type: ignore
            self.handler.setLevel(self.level)  # type: ignore
            self.handler.setFormatter(self.formatter)  # type: ignore
        else:
            super().set_context(ti)
        self.context_set = True
Exemplo n.º 4
0
    def set_context(self, ti):
        """
        Provide task_instance context to airflow task handler.
        :param ti: task instance object
        """
        super().set_context(ti)
        self.mark_end_on_close = not ti.raw

        if self.write_stdout:
            self.handler = logging.StreamHandler(stream=sys.__stdout__)
            self.handler.setLevel(self.level)
            if self.json_format and not ti.raw:
                self.handler.setFormatter(
                    JSONFormatter(self.formatter._fmt,
                                  json_fields=self.json_fields,
                                  extras={
                                      'dag_id':
                                      str(ti.dag_id),
                                      'task_id':
                                      str(ti.task_id),
                                      'execution_date':
                                      self._clean_execution_date(
                                          ti.execution_date),
                                      'try_number':
                                      str(ti.try_number)
                                  }))
            else:
                self.handler.setFormatter(self.formatter)
        else:
            super().set_context(ti)
    def set_context(self, ti):
        """
        Provide task_instance context to airflow task handler.

        :param ti: task instance object
        """
        self.mark_end_on_close = not ti.raw

        if self.json_format:
            self.formatter = JSONFormatter(
                self.formatter._fmt,  # pylint: disable=protected-access
                json_fields=self.json_fields,
                extras={
                    'dag_id': str(ti.dag_id),
                    'task_id': str(ti.task_id),
                    'execution_date':
                    self._clean_execution_date(ti.execution_date),
                    'try_number': str(ti.try_number)
                })

        if self.write_stdout:
            if self.context_set:
                # We don't want to re-set up the handler if this logger has
                # already been initialized
                return

            self.handler = logging.StreamHandler(stream=sys.__stdout__)
            self.handler.setLevel(self.level)
            self.handler.setFormatter(self.formatter)
        else:
            super().set_context(ti)
        self.context_set = True
Exemplo n.º 6
0
    def test_format_with_exception(self):
        """
        Test exception is included in the message when using JSONFormatter
        """
        try:
            raise RuntimeError("message")
        except RuntimeError:
            exc_info = sys.exc_info()

        log_record = makeLogRecord({
            "exc_info": exc_info,
            "message": "Some msg"
        })
        json_fmt = JSONFormatter(json_fields=["message"])

        log_fmt = json.loads(json_fmt.format(log_record))
        assert "message" in log_fmt
        assert "Traceback (most recent call last)" in log_fmt["message"]
        assert 'raise RuntimeError("message")' in log_fmt["message"]
Exemplo n.º 7
0
 def test_uses_time(self):
     """
     Test usesTime method from JSONFormatter
     """
     json_fmt_asctime = JSONFormatter(json_fields=["asctime", "label"])
     json_fmt_no_asctime = JSONFormatter(json_fields=["label"])
     self.assertTrue(json_fmt_asctime.usesTime())
     self.assertFalse(json_fmt_no_asctime.usesTime())
Exemplo n.º 8
0
 def test_json_formatter_is_not_none(self):
     """
     JSONFormatter instance  should return not none
     """
     json_fmt = JSONFormatter()
     self.assertIsNotNone(json_fmt)