Python 测试时如何查看气流操作员的日志
我创建了一个自定义Bash操作符,如下所示Python 测试时如何查看气流操作员的日志,python,logging,airflow,Python,Logging,Airflow,我创建了一个自定义Bash操作符,如下所示 from airflow.operators.bash_operator import BashOperator class CustomOperator(BashOperator): """ Custom bash operator that just write whatever it is given as stmt The actual operator is more complex """ d
from airflow.operators.bash_operator import BashOperator
class CustomOperator(BashOperator):
"""
Custom bash operator that just write whatever it is given as stmt
The actual operator is more complex
"""
def __init__(self, stmt, **kwargs):
cmd = 'echo %s > /path/to/some/file.txt' % stmt
super().__init__(bash_command=cmd, **kwargs)
然后我为这个操作符创建了一个测试
from datetime import datetime
from unittest import TestCase
from airflow import DAG
from airflow.models import TaskInstance
class CustomOperatorTestCase(TestCase):
def test_execute(self):
dag = DAG(dag_id='test', start_date=datetime.now())
stmt = "hello world"
task = CustomOperator(stmt=stmt, task_id='custom', dag=dag)
ti = TaskInstance(task=task, execution_date=datetime.now())
task.execute(ti.get_template_context())
with open('/path/to/some/file.txt', 'r') as f:
self.assert(f.read(), stmt)
到目前为止还不错,但让我们假设我的CustomOperator
中有一个错误。例如,我把echo
错发到eko
。我在控制台中得到的唯一信息是:
ERROR: test_execute (tests.operators.test_custom_operator.CustomOperatorTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/airflow/tests/operators/test_shp2pgsql_operator.py", line 26, in test_execute
result = task.execute(ti.get_template_context())
File "/usr/local/lib/python3.6/site-packages/airflow/operators/bash_operator.py", line 135, in execute
raise AirflowException("Bash command failed")
airflow.exceptions.AirflowException: Bash command failed
我没有办法调试。到目前为止,我唯一的解决方案是在web UI中触发包含此任务的dag,然后转到logs选项卡
测试时如何在控制台中查看操作员的日志 您可以使用气流测试命令+pysnooper。它将为您提供实例和详细的调试信息
如果你觉得答案有帮助,请投票表决。事实证明,我只需要在测试文件的开头添加一个处理程序到记录器
aiffort.task.operators
import logging
import sys
log = logging.getLogger("airflow.task.operators")
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.INFO)
log.addHandler(handler)
这很有趣,但理想情况下,我想要一个完全依赖unittest的解决方案。在我看来,这只是一个调整日志的问题,以便它打印到坚固