You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2022/09/28 09:12:55 UTC

[GitHub] [airflow] Bowrna commented on a diff in pull request #26400: Create a more efficient airflow dag test command that also has better local logging

Bowrna commented on code in PR #26400:
URL: https://github.com/apache/airflow/pull/26400#discussion_r982153234


##########
airflow/models/dag.py:
##########
@@ -2435,6 +2436,56 @@ def cli(self):
         args = parser.parse_args()
         args.func(args, self)
 
+    @provide_session
+    def test(self, execution_date: datetime | None = None, run_conf=None, session=None):
+        """Execute one single DagRun for a given DAG and execution date, using the DebugExecutor."""
+
+        def add_logger_if_needed(ti: TaskInstance):
+            """
+            Add a formatted logger to the taskinstance so all logs are surfaced to the command line instead
+            of into a task file. Since this is a local test run, it is much better for the user to see logs
+            in the command line, rather than needing to search for a log file.
+            Args:
+                ti: The taskinstance that will receive a logger
+
+            """
+            format = logging.Formatter("[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s")
+            handler = logging.StreamHandler(sys.stdout)
+            handler.level = logging.INFO
+            handler.setFormatter(format)
+            handler_classes = [h.__class__.__name__ for h in ti.log.handlers]
+            if "StreamHandler" not in handler_classes:  # only add log handler once
+                ti.log.addHandler(handler)
+
+        execution_date = execution_date or timezone.utcnow()
+        self.clear(
+            start_date=execution_date,
+            end_date=execution_date,
+            dag_run_state=False,  # type: ignore

Review Comment:
   I would like to fix this issue. Could you explain me more about this @uranusjr @dimberman ?



-- 
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-unsubscribe@airflow.apache.org

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