获取接收器

演示如何检索 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 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

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 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

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 示例浏览器