シンクを取得する

Cloud Logging シンクのメタデータを取得する方法について説明します。

コードサンプル

Go

Logging 用のクライアント ライブラリをインストールして使用する方法については、Logging クライアント ライブラリをご覧ください。

Logging への認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。

import (
	"context"
	"fmt"
	"io"

	"cloud.google.com/go/logging/logadmin"
)

// getSink retrieves the metadata for a Cloud Logging Sink.
func getSink(w io.Writer, projectID, sinkName string) error {
	ctx := context.Background()

	client, err := logadmin.NewClient(ctx, projectID)
	if err != nil {
		return err
	}
	defer client.Close()

	sink, err := client.Sink(ctx, sinkName)
	if err != nil {
		return err
	}
	fmt.Fprintf(w, "%v\n", sink)
	return nil
}

Java

Logging 用のクライアント ライブラリをインストールして使用する方法については、Logging クライアント ライブラリをご覧ください。

Logging への認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。

import com.google.cloud.logging.Logging;
import com.google.cloud.logging.LoggingOptions;
import com.google.cloud.logging.Sink;

/** Retrieve Cloud Logging Sink metadata. */
public class GetSinkMetadata {
  public static void main(String[] args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    // The Name of your sink
    String sinkName = "sink-name"; // i.e my-sink

    getSinkMetadata(sinkName);
  }

  public static void getSinkMetadata(String sinkName) throws Exception {
    // Instantiates a logging client
    try (Logging logging = LoggingOptions.getDefaultInstance().getService()) {

      Sink sink = logging.getSink(sinkName);

      // print sink metadata
      System.out.println("Name:" + sink.getName());
      System.out.println("Version Format:" + sink.getVersionFormat());
      System.out.println("Filter:" + sink.getFilter());
      System.out.println("Destination:" + sink.getDestination());
    }
  }
}

Node.js

Logging 用のクライアント ライブラリをインストールして使用する方法については、Logging クライアント ライブラリをご覧ください。

Logging への認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。

// Imports the Google Cloud client library
const {Logging} = require('@google-cloud/logging');

// Creates a client
const logging = new Logging();

/**
 * TODO(developer): Uncomment the following line to run the code.
 */
// const sinkName = 'Name of your sink, e.g. my-sink';

const sink = logging.sink(sinkName);

async function printSinkMetadata() {
  // See https://googleapis.dev/nodejs/logging/latest/Sink.html#getMetadata
  const [metadata] = await sink.getMetadata();
  console.log(`Name: ${metadata.name}`);
  console.log(`Destination: ${metadata.destination}`);
  console.log(`Filter: ${metadata.filter}`);
}
printSinkMetadata();

Python

Logging 用のクライアント ライブラリをインストールして使用する方法については、Logging クライアント ライブラリをご覧ください。

Logging への認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。

from typing import List
from google.cloud import logging

def get_sink(project_id: str, sink_name: str) -> logging.Sink:
    """Retrieves the metadata for a Cloud Logging Sink.

    Args:
        project_id: the ID of the project
        sink_name: the name of the sink

    Returns:
        A Cloud Logging Sink.
    """
    client = logging.Client(project=project_id)
    sink = client.sink(sink_name)
    sink.reload()
    print(f"Name: {sink.name}")
    print(f"Destination: {sink.destination}")
    print(f"Filter: {sink.filter_}")
    return sink

次のステップ

他の Google Cloud プロダクトに関連するコードサンプルの検索およびフィルタ検索を行うには、Google Cloud のサンプルをご覧ください。