Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 测试时如何查看气流操作员的日志_Python_Logging_Airflow - Fatal编程技术网

Python 测试时如何查看气流操作员的日志

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

我创建了一个自定义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
    """

    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。它将为您提供实例和详细的调试信息

  • 简单模式(仅使用气流测试命令)

    气流测试定制20190704 上面的命令将不依赖地运行任务。并将所有信息打印到您的控制台

  • 复杂模式(气流测试+pysnooper) 您需要通过pip install pysnooper提前安装pysonnper。在py文件中导入pysnooper,并将@pysnooper.snoop()decorator放在要调用的函数的正下方。 然后跑

    气流测试定制20190704

  • 您可以详细记录日志,每行执行和变量创建/更改信息将打印到控制台

    祝你好运
    如果你觉得答案有帮助,请投票表决。

    事实证明,我只需要在测试文件的开头添加一个处理程序到记录器
    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的解决方案。在我看来,这只是一个调整日志的问题,以便它打印到坚固