Search code examples
airflowairflow-taskflow

Logging 'logical_date' or 'ds' in Airflow task


I am using Airflow and writing my DAG with Task Flow API. This is an example of one: https://airflow.apache.org/docs/apache-airflow/stable/tutorial_taskflow_api.html

In one of my task functions, I want to log the logical date of my dag. Thus, inside my function I have the line logging.info("DAG logical date is: " + '{{ ds }}'). My log then reads: "DAG logical date is '{{ ds }}'". I am aware variables such as 'ds' can only be read inside templates. Is there a way to read these in a Python function which defines my Task?


Solution

  • You can to add ds as a parameter in your function See docs https://airflow.apache.org/docs/apache-airflow/stable/tutorial_taskflow_api.html#accessing-context-variables-in-decorated-tasks