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


##########
airflow/utils/log/task_context_logger.py:
##########
@@ -0,0 +1,120 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from __future__ import annotations
+
+import logging
+from copy import copy
+from functools import partial
+from typing import TYPE_CHECKING
+
+from airflow.config_templates.airflow_local_settings import 
TASK_CONTEXT_LOGGER_ENABLED
+
+if TYPE_CHECKING:
+    from airflow.models.taskinstance import TaskInstance
+
+
+logger = logging.getLogger(__name__)
+
+logging_levels = {
+    "info": logging.INFO,
+    "warning": logging.WARNING,
+    "error": logging.ERROR,
+    "critical": logging.CRITICAL,
+    "debug": logging.DEBUG,
+}
+
+
+class TaskContextLogger:
+    """
+    TaskContextLogger relays messages, typically in exceptional circumstances, 
to the task instance logs
+    from Airflow components e.g. the executor or scheduler.
+
+    :meta private:
+    """
+
+    def __init__(self, component_name: str):
+        """
+        Initialize the task context logger with the component name.
+
+        :param component_name: the name of the component that will be used to 
identify the log messages
+        """
+        self.component_name = component_name
+        self.task_handler = self._get_task_handler()
+        self.should_log = self._should_log()
+
+    def _should_log(self) -> bool:
+        if not TASK_CONTEXT_LOGGER_ENABLED:
+            return False
+        if not getattr(self.task_handler, "supports_task_context_logging", 
False):
+            logger.warning("Task handler does not support task context 
logging")
+            return False
+        return True
+
+    @staticmethod
+    def _get_task_handler():
+        """Returns the task handler that supports task context logging."""
+        handlers = [
+            handler
+            for handler in logging.getLogger("airflow.task").handlers
+            if getattr(handler, "supports_task_context_logging", False)
+        ]
+        return handlers[0] if handlers else None
+
+    def _log(
+        self,
+        ti: TaskInstance,
+        caller_logger: logging.Logger,
+        message: str,
+        *args,
+        level: str = "info",
+        **kwargs,
+    ):

Review Comment:
   I’m not sure about the argument ordering. Personally I’d prefer making `ti` 
and `caller_logger` kwarg-only, and make `level` the first argument, to better 
match the standard logger interface.
   
   I also wonder if `caller_logger` can be eliminated entirely since all 
current use cases only pass in `self.log` anyway, and this information can be 
obtained when the instance is constructed.



##########
airflow/utils/log/task_context_logger.py:
##########
@@ -0,0 +1,120 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from __future__ import annotations
+
+import logging
+from copy import copy
+from functools import partial
+from typing import TYPE_CHECKING
+
+from airflow.config_templates.airflow_local_settings import 
TASK_CONTEXT_LOGGER_ENABLED
+
+if TYPE_CHECKING:
+    from airflow.models.taskinstance import TaskInstance
+
+
+logger = logging.getLogger(__name__)
+
+logging_levels = {
+    "info": logging.INFO,
+    "warning": logging.WARNING,
+    "error": logging.ERROR,
+    "critical": logging.CRITICAL,
+    "debug": logging.DEBUG,
+}
+
+
+class TaskContextLogger:
+    """
+    TaskContextLogger relays messages, typically in exceptional circumstances, 
to the task instance logs
+    from Airflow components e.g. the executor or scheduler.
+
+    :meta private:
+    """
+
+    def __init__(self, component_name: str):
+        """
+        Initialize the task context logger with the component name.
+
+        :param component_name: the name of the component that will be used to 
identify the log messages
+        """
+        self.component_name = component_name
+        self.task_handler = self._get_task_handler()
+        self.should_log = self._should_log()
+
+    def _should_log(self) -> bool:
+        if not TASK_CONTEXT_LOGGER_ENABLED:
+            return False
+        if not getattr(self.task_handler, "supports_task_context_logging", 
False):
+            logger.warning("Task handler does not support task context 
logging")
+            return False
+        return True
+
+    @staticmethod
+    def _get_task_handler():
+        """Returns the task handler that supports task context logging."""
+        handlers = [
+            handler
+            for handler in logging.getLogger("airflow.task").handlers
+            if getattr(handler, "supports_task_context_logging", False)
+        ]
+        return handlers[0] if handlers else None
+
+    def _log(
+        self,
+        ti: TaskInstance,
+        caller_logger: logging.Logger,
+        message: str,
+        *args,
+        level: str = "info",
+        **kwargs,
+    ):

Review Comment:
   I’m not sure about the argument ordering. Personally I’d prefer making `ti` 
and `caller_logger` kwarg-only, and make `level` the first argument, to better 
match the standard logger interface.
   
   I also wonder if `caller_logger` can be eliminated entirely since all 
current use cases only pass in `self.log` anyway, and this information can be 
obtained when the instance is constructed.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to