将日志写入 Azure Blob 存储¶
Airflow 可以配置为在 Azure Blob 存储中读取和写入任务日志。它使用已有的 Airflow 连接来读取或写入日志。如果未正确设置连接,此过程将失败。
按照以下步骤启用 Azure Blob 存储日志记录
要启用此功能,需要按以下示例配置 airflow.cfg。
[logging]
# Airflow can store logs remotely in AWS S3, Azure Blob Storage, Google Cloud Storage or Elastic Search.
# Users must supply an Airflow connection id that provides access to the storage
# location. If remote_logging is set to true, see UPDATING.md for additional
# configuration requirements.
remote_logging = True
remote_base_log_folder = wasb://path/to/logs
[azure_remote_logging]
remote_wasb_log_container = my-container
注意
如果使用环境变量,则等效的配置如下
export AIRFLOW__LOGGING__REMOTE_LOGGING=True
export AIRFLOW__LOGGING__REMOTE_LOG_CONN_ID=<your_wasb_connection_id>
export AIRFLOW__LOGGING__REMOTE_BASE_LOG_FOLDER=wasb://path/to/logs
export AIRFLOW__AZURE_REMOTE_LOGGING__REMOTE_WASB_LOG_CONTAINER=<your_container_name>
remote_base_log_folder 必须以 wasb:// 为前缀,以便 Airflow 使用正确的日志处理程序。格式不正确可能导致误导性的 ResourceNotFoundError,即使容器已存在。
设置步骤:¶
使用
pip install apache-airflow-providers-microsoft-azure安装提供程序包。确保 连接 已经设置好,对 Azure Blob 存储的
remote_wasb_log_container容器以及路径remote_base_log_folder具备读写权限。该连接应配置相应的身份验证凭证(例如账户密钥、共享访问密钥或托管身份)。对于账户密钥身份验证,您可以在连接的 extra 字段中添加account_key,格式为 JSON 字典:{\"account_key\": \"your_account_key\"}。设置上述配置值。请注意,容器应已存在。
重启 Airflow web 服务器和调度器,并触发(或等待)新任务的执行。
验证在您定义的指定基础路径下的容器中,已出现新执行任务的日志。
验证 UI 中的 Azure Blob 存储查看器是否正常工作。打开一个新执行的任务,确认您看到类似如下内容:
*** Found remote logs:
*** * https://my-container.blob.core.windows.net/path/to/logs/dag_id=tutorial_dag/run_id=manual.../task_id=load/attempt=1.log
[2023-07-23, 03:52:47] {taskinstance.py:1144} INFO - Dependencies all met...
注意 远程日志文件的路径列在第二行。