List sinks

Demonstrates how to list Cloud Logging Sinks.

Code sample

C#

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.

private void ListSinks()
{
    var sinkClient = ConfigServiceV2Client.Create();
    ProjectName projectName = new ProjectName(s_projectId);
    var listOfSinks = sinkClient.ListSinks(projectName, callSettings: _retryAWhile);
    foreach (var sink in listOfSinks)
    {
        Console.WriteLine($"{sink.Name} {sink.ToString()}");
    }
}

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"
	"log"

	"google.golang.org/api/iterator"

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

func listSinks(projectID string) ([]string, error) {
	ctx := context.Background()
	client, err := logadmin.NewClient(ctx, projectID)
	if err != nil {
		log.Fatalf("logadmin.NewClient: %v", err)
	}
	defer client.Close()

	var sinks []string
	it := client.Sinks(ctx)
	for {
		sink, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return nil, err
		}
		sinks = append(sinks, sink.ID)
	}
	return sinks, 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.

Page<Sink> sinks = logging.listSinks(ListOption.pageSize(100));
for (Sink sink : sinks.iterateAll()) {
  // do something with the sink
}

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();

async function printSinkMetadata() {
  // See https://googleapis.dev/nodejs/logging/latest/Logging.html#getSinks
  const [sinks] = await logging.getSinks();
  console.log('Sinks:');
  sinks.forEach(sink => {
    console.log(sink.name);
    console.log(`  Destination: ${sink.metadata.destination}`);
    console.log(`  Filter: ${sink.metadata.filter}`);
  });
}
printSinkMetadata();

PHP

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.

use Google\Cloud\Logging\LoggingClient;

/**
 * List log sinks.
 *
 * @param string $projectId
 */
function list_sinks($projectId)
{
    $logging = new LoggingClient(['projectId' => $projectId]);
    $sinks = $logging->sinks();
    foreach ($sinks as $sink) {
        /* @var $sink \Google\Cloud\Logging\Sink */
        foreach ($sink->info() as $key => $value) {
            printf('%s:%s' . PHP_EOL,
                $key,
                is_string($value) ? $value : var_export($value, true)
            );
        }
        print PHP_EOL;
    }
}

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.

def list_sinks():
    """Lists all sinks."""
    logging_client = logging.Client()

    sinks = list(logging_client.list_sinks())

    if not sinks:
        print("No sinks.")

    for sink in sinks:
        print("{}: {} -> {}".format(sink.name, sink.filter_, sink.destination))

Ruby

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.

require "google/cloud/logging"

logging = Google::Cloud::Logging.new

logging.sinks.each do |sink|
  puts "#{sink.name}: #{sink.filter} -> #{sink.destination}"
end

What's next

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