dstandish commented on code in PR #32646:
URL: https://github.com/apache/airflow/pull/32646#discussion_r1266297802


##########
airflow/utils/log/file_task_handler.py:
##########
@@ -182,14 +184,38 @@ def set_context(self, ti: TaskInstance) -> None | 
SetContextPropagate:
         functionality is only used in unit testing.
 
         :param ti: task instance object
+        :param identifier: if set, adds suffix to log file. For use when 
shipping exceptional messages
+            to task logs from context other than task or trigger run
         """
-        local_loc = self._init_file(ti)
+        local_loc = self._init_file(ti, identifier=identifier)
         self.handler = NonCachingFileHandler(local_loc, encoding="utf-8")
         if self.formatter:
             self.handler.setFormatter(self.formatter)
         self.handler.setLevel(self.level)
         return SetContextPropagate.MAINTAIN_PROPAGATE if 
self.maintain_propagate else None
 
+    @cached_property
+    def _supports_arbitrary_ship(self):
+        return "identifier" in inspect.signature(self.set_context).parameters
+
+    def ship_arbitrary_message(
+        self, *, ti: TaskInstanceKey | TaskInstance, identifier: str, message: 
str, level: int
+    ):
+        """For sending exception messages to task logs visible in UI."""
+        if not self._supports_arbitrary_ship:
+            return
+        h = copy(self)
+        try:
+            if hasattr(h, "mark_end_on_close"):
+                h.mark_end_on_close = False
+            h.set_context(ti, identifier=identifier)
+            filename, lineno, func, stackinfo = logger.findCaller()
+            record = logging.LogRecord("", level, filename, lineno, message, 
None, None, func=func)
+            h.emit(record)
+            logger.warning("EMITTED!!!")

Review Comment:
   ```suggestion
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@airflow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to