如何在Airflow中的PythonOperator中登录Python函数

时间:2018-08-26 00:37:08

标签: airflow

我在Airflow DAG中使用PythonOperator,并且需要在操作员的Python函数中打印某些内容。我尝试打印,但显然没有解决。不确定这样做是否可行。接下来,我尝试在PythonOperator中传递self.log,但不确定如何传递该引用。

task = PythonOperator(
    task_id='task1',
    python_callable=my_func,
    params={ ... },
    provide_context=True,
    dag=dag
)

...

def my_func(**context):
    ...
    print(some_message) # this didn't work.

谢谢!

1 个答案:

答案 0 :(得分:4)

您应该通过598模块登录Airflow(通常在Python中)登录方式,即

logging

位于DAG定义的顶部,

import logging

代替logging.info(some_message) 函数中的print语句。您可以在Python文档中找到my_func之外可以使用的其他功能(具有不同的日志记录级别/关键性):https://docs.python.org/3/howto/logging.html#logging-basic-tutorial