将日志写入 Google Cloud Storage¶
将日志写入 Google Cloud Storage 的远程日志功能使用已存在的 Airflow 连接来读取或写入日志。如果没有正确配置连接,此过程将会失败。
请按照以下步骤启用 Google Cloud Storage 日志记录。
要启用此功能,需要按以下示例配置 airflow.cfg。
[logging]
# Airflow can store logs remotely in AWS S3, 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 = gs://my-bucket/path/to/logs
remote_log_conn_id = my_gcs_conn
默认情况下会使用 Application Default Credentials(应用默认凭证)获取凭证。如果想使用自己的服务账号,也可以在
[logging]部分设置google_key_path选项。确保使用这些凭证时,你能够对上面在
remote_base_log_folder中定义的 Google Cloud Storage 存储桶拥有读写权限。安装
google包,例如:pip install 'apache-airflow[google]'。重启 Airflow web 服务器和调度器,并触发(或等待)新任务的执行。
验证在你定义的存储桶中,新执行的任务日志是否已经出现。
验证 UI 中的 Google Cloud Storage 查看器是否工作正常。打开一个新执行的任务,确认你看到类似下面的内容:
*** Reading remote log from gs://<bucket where logs should be persisted>/example_bash_operator/run_this_last/2017-10-03T00:00:00/16.log.
[2017-10-03 21:57:50,056] {cli.py:377} INFO - Running on host chrisr-00532
[2017-10-03 21:57:50,093] {base_task_runner.py:115} INFO - Running: ['bash', '-c', 'airflow tasks run example_bash_operator run_this_last 2017-10-03T00:00:00 --job-id 47 --raw -S DAGS_FOLDER/example_dags/example_bash_operator.py']
[2017-10-03 21:57:51,264] {base_task_runner.py:98} INFO - Subtask: [2017-10-03 21:57:51,263] {__init__.py:45} INFO - Using executor SequentialExecutor
[2017-10-03 21:57:51,306] {base_task_runner.py:98} INFO - Subtask: [2017-10-03 21:57:51,306] {models.py:186} INFO - Filling up the DagBag from /airflow/dags/example_dags/example_bash_operator.py
注意,远程日志文件的路径会列在第一行。
字段 remote_logging 的值必须始终设为 True,此功能才能生效。关闭此选项将导致日志不被发送到 GCS。
remote_base_log_folder 选项包含用于指定所使用处理程序类型的 URL。要与 GCS 集成,此选项应以 gs:// 开头。URL 的路径部分指定了 GCS 中日志对象的存储桶和前缀,例如 gs://my-bucket/path/to/logs 会在 my-bucket 中写入前缀为 path/to/logs 的日志。
你可以在 [logging] 部分设置 google_key_path 选项,以指定服务账号密钥文件的路径。如果省略,则会使用基于应用默认凭证的身份验证和授权。请确保使用这些凭证时,你能够读取和写入日志。
注意
上述凭证 **不是** 你在 google_cloud_default 连接中配置的同一套凭证。它们通常应当与 google_cloud_default 的凭证不同,仅拥有读取和写入日志的权限。出于安全考虑,限制日志读取器的访问仅能读取/写入日志是非常重要的安全措施。