Get a sink

Demonstrates how to retrieve the metadata for a Cloud Logging Sink.

Code sample

Go

To learn how to install and use the client library for Logging, see Logging client libraries.

To authenticate to Logging, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

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

To learn how to install and use the client library for Logging, see Logging client libraries.

To authenticate to Logging, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

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

To learn how to install and use the client library for Logging, see Logging client libraries.

To authenticate to Logging, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

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

To learn how to install and use the client library for Logging, see Logging client libraries.

To authenticate to Logging, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

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

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.