python - 使用自定义文件名进行 Airflow 记录?

标签 python python-3.x logging airflow

我正在使用 Apache Airflow 1.10.9(基于 puckel/docker-airflow docker 镜像)通过 BashOperator 在 DAG 中运行多个 Python 脚本。日志当前写入 /usr/local/airflow/logs

是否可以将 Airflow 配置为

  1. 将日志写入另一个目录,例如 /home/foo/logs
  2. 日志应仅包含 python 脚本的标准输出
  3. 日志应存储在以下目录/文件名格式中:

    /home/foo/logs/[execution-date]-[dag-id]-[task-id].log
    
  4. 如果可能,重试应附加到同一 .log 文件中。否则,我们可以采用命名约定:

    /home/foo/logs/[execution-date]-[dag-id]-[task-id]-[retry-number].log
    

谢谢大家!

DAG 示例

from airflow import DAG
from airflow.operators.bash_operator import BashOperator

default_args = { ... }

dag = DAG(
    'mydag',
    default_args=default_args,
    schedule_interval='*/10 * * * *',
)

# Log to /home/foo/logs/2020-05-12-mydag-hello_world.log
t1 = BashOperator(
    task_id='hello_world',
    bash_command='/path/to/env/bin/python /path/to/scripts/hello_world.py',
    dag=dag,
)


# Log to /home/foo/logs/2020-05-12-mydag-hey_there.log
t2 = BashOperator(
    task_id='hey_there',
    bash_command='/path/to/env/bin/python /path/to/scripts/hey_there.py',
    dag=dag,
)

t1 >> t2

最佳答案

https://bcb.github.io/airflow/run-dag-and-watch-logs

此链接有答案。

设置 FILENAME_TEMPLATE 设置。

导出 AIRFLOW__CORE__LOG_FILENAME_TEMPLATE="{{ ti.dag_id }}.log"

您可以编辑airflow.cfg文件 log_filename_template 变量

在那里添加任何与 Airflow 相关的变量。

关于python - 使用自定义文件名进行 Airflow 记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61757827/

相关文章:

Python 类和命名空间?

python - 检查行值 value 是否等于列名并访问该列的值

java - 我什么时候应该使用 error.printStrackTrace() 而不是 log4.j.error()

python - 正则表达式匹配字符串后跟任何单词后跟字符串变量

python - tkinter 文本小部件 : how to indent automatically after a soft line wrap

python - 尝试使用 Python 3.7.2 pip 安装包会导致 TSL/SSL 错误

python - 预期 Python 中的预期 block

cakephp - 在生产模式下写入 CakePHP 自定义日志文件

当级别严重时,Java 日志记录不执行信息

python - 名称错误 : name 'WD_COLOR_INDEX' is not defined while using python-docx