Logeinträge für eine Übertragungsausführung abrufen

Ruft alle Log-Nachrichten ab, die während einer bestimmten BigQuery Data Transfer Service-Übertragungsausführung generiert wurden.

Codebeispiel

Node.js

Bevor Sie dieses Beispiel anwenden, folgen Sie den Schritten zur Einrichtung von Node.js in der BigQuery-Kurzanleitung zur Verwendung von Clientbibliotheken. Weitere Angaben finden Sie in der Referenzdokumentation zur BigQuery Node.js API.

Richten Sie zur Authentifizierung bei BigQuery die Standardanmeldedaten für Anwendungen ein. Weitere Informationen finden Sie unter Authentifizierung für Clientbibliotheken einrichten.

const {
  DataTransferServiceClient,
} = require('@google-cloud/bigquery-data-transfer');
const {status} = require('@grpc/grpc-js');

const client = new DataTransferServiceClient();

/**
 * Lists log messages for a transfer run.
 * Transfer runs are created for each transfer configuration, and they may have associated log messages.
 *
 * @param {string} projectId Google Cloud project ID (for example, 'example-project-id').
 * @param {string} location The geographic location of the transfer configuration (for example, 'us-central1').
 * @param {string} transferConfigId The transfer configuration ID (for example, '1234a-5678-9b12c').
 * @param {string} runId The transfer run ID (for example, 'd123e-4567-89b0c-1d23e').
 */
async function listTransferLogs(projectId, location, transferConfigId, runId) {
  const parent = client.projectLocationTransferConfigRunPath(
    projectId,
    location,
    transferConfigId,
    runId,
  );
  const request = {
    parent,
  };

  try {
    const [logs] = await client.listTransferLogs(request);

    if (logs.length === 0) {
      console.error(`No transfer logs found for ${parent}.`);
      return;
    }

    console.log(`Found ${logs.length} transfer log entries:`);
    for (const log of logs) {
      console.log(
        `  [${log.severity}] ${new Date(
          log.messageTime.seconds * 1000,
        ).toISOString()}: ${log.messageText}`,
      );
    }
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Transfer run not found: ${runId}`);
    } else {
      console.error('Error listing transfer logs:', err);
    }
  }
}

Python

Bevor Sie dieses Beispiel ausprobieren, folgen Sie der Python-Einrichtungsanleitung in der BigQuery-Kurzanleitung zur Verwendung von Clientbibliotheken. Weitere Angaben finden Sie in der Referenzdokumentation zur BigQuery Python API.

Richten Sie zur Authentifizierung bei BigQuery die Standardanmeldedaten für Anwendungen ein. Weitere Informationen finden Sie unter Authentifizierung für Clientbibliotheken einrichten.


import google.api_core.exceptions
from google.cloud import bigquery_datatransfer_v1

client = bigquery_datatransfer_v1.DataTransferServiceClient()


def list_transfer_logs(
    project_id: str, location: str, transfer_config_id: str, run_id: str
) -> None:
    """Prints the transfer logs for a given transfer run.

    This sample shows how to retrieve the logs for a specific transfer run,
    which can be useful for debugging and monitoring transfer jobs.

    Args:
        project_id: The Google Cloud project ID.
        location: The geographic location of the transfer configuration (e.g., 'us-central1').
        transfer_config_id: The ID of the transfer configuration.
        run_id: The ID of the transfer run.
    """

    parent = client.run_path(
        f"{project_id}/locations/{location}", transfer_config_id, run_id
    )

    try:
        pager = client.list_transfer_logs(parent=parent)

        print(f"Logs for transfer run {run_id}:")
        for log in pager:
            print(f"  - {log.severity.name}: {log.message_text}")

    except google.api_core.exceptions.NotFound:
        print(
            f"Error: Transfer run '{run_id}' not found in project '{project_id}' location '{location}'."
        )

Weitere Informationen

Wenn Sie nach Codebeispielen für andere Produkte von Google Cloud suchen und filtern möchten, können Sie den Beispielbrowser fürGoogle Cloud verwenden.