Retrieves all log messages produced during a specific BigQuery Data Transfer Service transfer run.

## Code sample

### Java


Before trying this sample, follow the Java setup instructions in the
[BigQuery quickstart using
client libraries](https://docs.cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries).


For more information, see the
[BigQuery Java API
reference documentation](https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/overview).


To authenticate to BigQuery, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/bigquery/docs/authentication#client-libs).

    import com.google.api.gax.rpc.https://docs.cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.ApiException.html;
    import com.google.api.gax.rpc.https://docs.cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.NotFoundException.html;
    import com.google.cloud.bigquery.datatransfer.v1.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquerydatatransfer/latest/com.google.cloud.bigquery.datatransfer.v1.DataTransferServiceClient.html;
    import com.google.cloud.bigquery.datatransfer.v1.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquerydatatransfer/latest/com.google.cloud.bigquery.datatransfer.v1.RunName.html;
    import com.google.cloud.bigquery.datatransfer.v1.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquerydatatransfer/latest/com.google.cloud.bigquery.datatransfer.v1.TransferMessage.html;
    import java.io.IOException;

    public class ListTransferLogs {

      /**
       * Prints the transfer logs for a given transfer run.
       *
       * @param projectId The Google Cloud project ID (for example, "example-project-id").
       * @param location The geographic location of the transfer configuration (for example,
       *     "us-central1").
       * @param transferConfigId The ID of the transfer configuration (for example,
       *     "1234a-5678-90b1-2c3d-4e5f67890g12").
       * @param runId The ID of the transfer run (for example, "6789b-0123-45c6-7d8e-9f0a12345b67").
       */
      public static void listTransferLogs(
          String projectId, String location, String transferConfigId, String runId) {
        https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquerydatatransfer/latest/com.google.cloud.bigquery.datatransfer.v1.RunName.html parent =
            https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquerydatatransfer/latest/com.google.cloud.bigquery.datatransfer.v1.RunName.html.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquerydatatransfer/latest/com.google.cloud.bigquery.datatransfer.v1.RunName.html#com_google_cloud_bigquery_datatransfer_v1_RunName_ofProjectLocationTransferConfigRunName_java_lang_String_java_lang_String_java_lang_String_java_lang_String_(
                projectId, location, transferConfigId, runId);

        try (https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquerydatatransfer/latest/com.google.cloud.bigquery.datatransfer.v1.DataTransferServiceClient.html client = https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquerydatatransfer/latest/com.google.cloud.bigquery.datatransfer.v1.DataTransferServiceClient.html.create()) {
          System.out.printf("Logs for transfer run %s:%n", runId);
          for (https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquerydatatransfer/latest/com.google.cloud.bigquery.datatransfer.v1.TransferMessage.html log : client.listTransferLogs(parent).iterateAll()) {
            System.out.printf("  - %s: %s%n", log.getSeverity(), log.getMessageText());
          }
        } catch (https://docs.cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.NotFoundException.html err) {
          System.err.printf(
              "error: transfer run '%s' not found in project '%s' location '%s': %s%n",
              runId, projectId, location, err.https://docs.cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.ErrorDetails.html#com_google_api_gax_rpc_ErrorDetails__T_getMessage_java_lang_Class_T__());
        } catch (https://docs.cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.ApiException.html | IOException err) {
          System.err.printf(
              "error: failed to list transfer logs for '%s': %s%n", parent, err.https://docs.cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.ErrorDetails.html#com_google_api_gax_rpc_ErrorDetails__T_getMessage_java_lang_Class_T__());
        }
      }

### Node.js


Before trying this sample, follow the Node.js setup instructions in the
[BigQuery quickstart using
client libraries](https://docs.cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries).


For more information, see the
[BigQuery Node.js API
reference documentation](https://googleapis.dev/nodejs/bigquery/latest/index.html).


To authenticate to BigQuery, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/bigquery/docs/authentication#client-libs).

    const {
      DataTransferServiceClient,
    } = require('https://docs.cloud.google.com/nodejs/docs/reference/bigquery-data-transfer/latest/overview.html');
    const {status} = require('@grpc/grpc-js');

    const client = new https://docs.cloud.google.com/nodejs/docs/reference/bigquery-data-transfer/latest/overview.html();

    /**
     * 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.https://docs.cloud.google.com/nodejs/docs/reference/bigquery-data-transfer/latest/bigquery-data-transfer/v1.datatransferserviceclient.html(
        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


Before trying this sample, follow the Python setup instructions in the
[BigQuery quickstart using
client libraries](https://docs.cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries).


For more information, see the
[BigQuery Python API
reference documentation](https://docs.cloud.google.com/python/docs/reference/bigquery/latest).


To authenticate to BigQuery, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/bigquery/docs/authentication#client-libs).


    import google.api_core.exceptions
    from google.cloud import https://docs.cloud.google.com/python/docs/reference/bigquerydatatransfer/latest

    client = https://docs.cloud.google.com/python/docs/reference/bigquerydatatransfer/latest.https://docs.cloud.google.com/python/docs/reference/bigquerydatatransfer/latest/google.cloud.bigquery_datatransfer_v1.services.data_transfer_service.DataTransferServiceClient.html()


    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.https://docs.cloud.google.com/python/docs/reference/bigquerydatatransfer/latest/google.cloud.bigquery_datatransfer_v1.services.data_transfer_service.DataTransferServiceClient.html#google_cloud_bigquery_datatransfer_v1_services_data_transfer_service_DataTransferServiceClient_run_path(
            f"{project_id}/locations/{location}", transfer_config_id, run_id
        )

        try:
            pager = client.https://docs.cloud.google.com/python/docs/reference/bigquerydatatransfer/latest/google.cloud.bigquery_datatransfer_v1.services.data_transfer_service.DataTransferServiceClient.html#google_cloud_bigquery_datatransfer_v1_services_data_transfer_service_DataTransferServiceClient_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}'."
            )

## What's next


To search and filter code samples for other Google Cloud products, see the
[Google Cloud sample browser](https://docs.cloud.google.com/docs/samples?product=bigquerydatatransferservice).