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


##########
airflow/cli/commands/task_command.py:
##########
@@ -281,38 +283,62 @@ def _extract_external_executor_id(args) -> str | None:
 
 
 @contextmanager
-def _capture_task_logs(ti: TaskInstance) -> Generator[None, None, None]:
+def _move_task_handlers_to_root(ti: TaskInstance) -> Generator[None, None, 
None]:
     """
-    Manage logging context for a task run.
+    Move handlers for task logging to root logger.
 
-    - Replace the root logger configuration with the airflow.task configuration
-      so we can capture logs from any custom loggers used in the task.
+    We want anything logged during task run to be propagated to task log 
handlers.
+    If running in a k8s executor pod, also keep the stream handler on root 
logger
+    so that logs are still emitted to stdout.
+    """
+    # nothing to do
+    if not ti.log.handlers or settings.DONOT_MODIFY_HANDLERS:
+        yield
+        return
+
+    def get_console_handler(logger):
+        for h in logger.handlers:
+            if h.name == "console":
+                return h
+
+    def ensure_handler(logger, handler):
+        if not handler:
+            return
+        if handler not in logger.handlers:
+            logger.addHandler(handler)
+
+    # Move task handlers to root and reset task logger and restore original 
logger settings after exit.
+    # If k8s executor, we need to ensure that root logger has a console 
handler, so that
+    # task logs propagate to stdout (this is how webserver retrieves them 
while task is running).
+    root_logger = logging.getLogger()
+    console_handler = get_console_handler(root_logger)
+    with LoggerMutationHelper(root_logger), LoggerMutationHelper(ti.log) as 
task_helper:
+        task_helper.move(root_logger)
+        if IS_K8S_EXECUTOR_POD:

Review Comment:
   a noble goal. but i would say out of scope for this PR.  there already is 
this env var.  and if executor provides an interface we can use here, we can 
later update to use it.



-- 
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