Senke abrufen

Veranschaulicht das Abrufen der Metadaten für eine Cloud Logging-Senke

Codebeispiel

Go

Informationen zum Installieren und Verwenden der Clientbibliothek für Logging finden Sie unter Logging-Clientbibliotheken.

Richten Sie Standardanmeldedaten für Anwendungen ein, um sich bei Logging zu authentifizieren. Weitere Informationen finden Sie unter Authentifizierung für eine lokale Entwicklungsumgebung einrichten.

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

Informationen zum Installieren und Verwenden der Clientbibliothek für Logging finden Sie unter Logging-Clientbibliotheken.

Richten Sie Standardanmeldedaten für Anwendungen ein, um sich bei Logging zu authentifizieren. Weitere Informationen finden Sie unter Authentifizierung für eine lokale Entwicklungsumgebung einrichten.

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

Informationen zum Installieren und Verwenden der Clientbibliothek für Logging finden Sie unter Logging-Clientbibliotheken.

Richten Sie Standardanmeldedaten für Anwendungen ein, um sich bei Logging zu authentifizieren. Weitere Informationen finden Sie unter Authentifizierung für eine lokale Entwicklungsumgebung einrichten.

// 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

Informationen zum Installieren und Verwenden der Clientbibliothek für Logging finden Sie unter Logging-Clientbibliotheken.

Richten Sie Standardanmeldedaten für Anwendungen ein, um sich bei Logging zu authentifizieren. Weitere Informationen finden Sie unter Authentifizierung für eine lokale Entwicklungsumgebung einrichten.

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

Nächste Schritte

Informationen zum Suchen und Filtern von Codebeispielen für andere Google Cloud-Produkte finden Sie im Google Cloud-Beispielbrowser.