使用日志分析作业

本页面介绍如何在 Cloud Logging 中为作业启用和查看日志

您可以使用日志来获取对分析作业有用的信息。例如,日志可以帮助您调试失败的作业。 为作业启用 Cloud Logging 后,Cloud Logging 会生成以下类型的日志供您查看:

  • 任务日志 (batch_task_logs):写入标准输出 (stdout) 和标准错误 (stderr) 流中的任何数据的日志。如需为作业生成任务日志,请配置任务,以将数据写入这些信息流以供分析和调试。
  • 代理日志 (batch_agent_logs):Batch 服务代理中活动的日志。Batch 会自动为您的作业生成这些日志。

Cloud Logging 仅在作业开始运行之后生成日志。如需验证作业是否已开始运行,请查看作业的详细信息,并确认作业的state是否为 RUNNING 或更高的状态。如果您需要分析未生成日志的作业(例如由于作业在处于 RUNNING 状态之前失败),请使用 gcloud CLI 或 Batch API 查看作业的详细信息,然后检查 statusEvents 字段

准备工作

为作业启用日志

如需为作业生成日志,请在创建作业时启用 Cloud Logging:

  • 如果您使用 Google Cloud 控制台创建作业,则 Cloud Logging 始终处于启用状态。
  • 如果您使用 gcloud CLI 或 Batch API 创建作业,系统会默认停用 Cloud Logging。 如需启用 Cloud Logging,请在创建作业时为 logsPolicy 字段添加以下配置:

    {
        ...
        "logsPolicy": {
            "destination": "CLOUD_LOGGING"
        }
        ...
    }
    

查看作业的日志

您可以使用 Google Cloud 控制台、gcloud CLI、Logging API、Go、Java、Python 或 C++ 查看作业的日志。

控制台

如需使用 Google Cloud 控制台查看作业的日志,请执行以下操作:

  1. 在 Google Cloud 控制台中,转到作业列表页面。

    转到“作业”列表

  2. 作业名称列中,点击作业的名称。 此时系统会打开作业详情页面。

  3. 点击日志标签页。Batch 会显示与作业关联的所有日志。

  4. 可选:如需过滤日志,请执行以下操作之一:

gcloud

如需使用 gcloud CLI 查看日志,请使用 gcloud logging read 命令

gcloud logging read "QUERY"

其中 QUERY 是针对包含批量过滤器参数的批处理日志的查询。

API

如需使用 Logging API 查看日志,请使用 entries.list 方法

POST https://logging.googleapis.com/v2/entries:list
{
    "resourceNames": [
        "projects/PROJECT_ID"
    ],
    "filter": "QUERY"
    "orderBy": "timestamp desc"
}

替换以下内容:

Go

Go

如需了解详情,请参阅 Batch Go API 参考文档

要向 Batch 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证

import (
	"context"
	"fmt"
	"io"

	batch "cloud.google.com/go/batch/apiv1"
	"cloud.google.com/go/logging"
	"cloud.google.com/go/logging/logadmin"
	"google.golang.org/api/iterator"
	batchpb "google.golang.org/genproto/googleapis/cloud/batch/v1"
)

// Retrieve the logs written by the given job to Cloud Logging
func printJobLogs(w io.Writer, projectID string, job *batchpb.Job) error {
	// projectID := "your_project_id"

	ctx := context.Background()
	batchClient, err := batch.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("NewClient: %w", err)
	}
	defer batchClient.Close()

	adminClient, err := logadmin.NewClient(ctx, projectID)
	if err != nil {
		return fmt.Errorf("Failed to create logadmin client: %w", err)
	}
	defer adminClient.Close()

	const name = "batch_task_logs"

	iter := adminClient.Entries(ctx,
		// Only get entries from the "batch_task_logs" log for the job with the given UID
		logadmin.Filter(fmt.Sprintf(`logName = "projects/%s/logs/%s" AND labels.job_uid=%s`, projectID, name, job.Uid)),
	)

	var entries []*logging.Entry

	for {
		logEntry, err := iter.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return fmt.Errorf("unable to fetch log entry: %w", err)
		}
		entries = append(entries, logEntry)
		fmt.Fprintf(w, "%s\n", logEntry.Payload)
	}

	fmt.Fprintf(w, "Successfully fetched %d log entries\n", len(entries))

	return nil
}

Java

Java

如需了解详情,请参阅 Batch Java API 参考文档

要向 Batch 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证

import com.google.cloud.batch.v1.Job;
import com.google.cloud.logging.v2.LoggingClient;
import com.google.logging.v2.ListLogEntriesRequest;
import com.google.logging.v2.LogEntry;
import java.io.IOException;

public class ReadJobLogs {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Cloud project hosting the job.
    String projectId = "YOUR_PROJECT_ID";

    // The job which logs you want to print.
    Job job = Job.newBuilder().build();

    readJobLogs(projectId, job);
  }

  // Prints the log messages created by given job.
  public static void readJobLogs(String projectId, Job job) throws IOException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the `loggingClient.close()` method on the client to safely
    // clean up any remaining background resources.
    try (LoggingClient loggingClient = LoggingClient.create()) {

      ListLogEntriesRequest request = ListLogEntriesRequest.newBuilder()
          .addResourceNames(String.format("projects/%s", projectId))
          .setFilter(String.format("labels.job_uid=%s", job.getUid()))
          .build();

      for (LogEntry logEntry : loggingClient.listLogEntries(request).iterateAll()) {
        System.out.println(logEntry.getTextPayload());
      }
    }
  }
}

Python

Python

如需了解详情,请参阅 Batch Python API 参考文档

要向 Batch 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证

from __future__ import annotations

from typing import NoReturn

from google.cloud import batch_v1
from google.cloud import logging

def print_job_logs(project_id: str, job: batch_v1.Job) -> NoReturn:
    """
    Prints the log messages created by given job.

    Args:
        project_id: name of the project hosting the job.
        job: the job which logs you want to print.
    """
    # Initialize client that will be used to send requests across threads. This
    # client only needs to be created once, and can be reused for multiple requests.
    log_client = logging.Client(project=project_id)
    logger = log_client.logger("batch_task_logs")

    for log_entry in logger.list_entries(filter_=f"labels.job_uid={job.uid}"):
        print(log_entry.payload)

C++

C++

如需了解详情,请参阅 Batch C++ API 参考文档

要向 Batch 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证

#include "google/cloud/batch/v1/batch_client.h"
#include "google/cloud/logging/v2/logging_service_v2_client.h"
#include "google/cloud/location.h"
#include "google/cloud/project.h"

  [](std::string const& project_id, std::string const& location_id,
     std::string const& job_id) {
    auto const project = google::cloud::Project(project_id);
    auto const location = google::cloud::Location(project, location_id);
    auto const name = location.FullName() + "/jobs/" + job_id;
    auto batch = google::cloud::batch_v1::BatchServiceClient(
        google::cloud::batch_v1::MakeBatchServiceConnection());
    auto job = batch.GetJob(name);
    if (!job) throw std::move(job).status();

    auto logging = google::cloud::logging_v2::LoggingServiceV2Client(
        google::cloud::logging_v2::MakeLoggingServiceV2Connection());
    auto const log_name = project.FullName() + "/logs/batch_task_logs";
    google::logging::v2::ListLogEntriesRequest request;
    request.mutable_resource_names()->Add(project.FullName());
    request.set_filter("logName=\"" + log_name +
                       "\" labels.job_uid=" + job->uid());
    for (auto l : logging.ListLogEntries(request)) {
      if (!l) throw std::move(l).status();
      std::cout << l->text_payload() << "\n";
    }
  }

过滤 Batch 日志

您可以通过编写包含以下一个或多个过滤条件参数以及零个或零个以上布尔值运算符(ANDORNOT的查询来过滤 Batch 日志。

  • 如需过滤来自特定作业的日志,请指定作业的唯一 ID (UID):

    labels.job_uid=JOB_UID
    

    其中,JOB_UID 是作业的 UID。如需获取作业的 UID,请查看作业的详细信息

  • 如需过滤特定类型的 Batch 日志,请指定日志类型:

    logName=projects/PROJECT_ID/logs/BATCH_LOG_TYPE
    

    替换以下内容:

    • PROJECT_ID:要查看其日志的项目的ID
    • BATCH_LOG_TYPE:要查看的 Batch 日志类型,batch_task_logs 表示任务日志,batch_agent_logs 表示代理日志。

后续步骤