Listar registros

Demonstra como listar os nomes dos registros disponíveis.

Exemplo de código

Go

Para saber como instalar e usar a biblioteca de cliente para o Logging, consulte Bibliotecas de cliente do Logging.

Para autenticar no Logging, configure o Application Default Credentials. Para mais informações, consulte Configurar a autenticação para um ambiente de desenvolvimento local.

Ver no GitHub (em inglês) Feedback

import (
	"context"
	"fmt"
	"io"

	"cloud.google.com/go/logging/logadmin"
	"google.golang.org/api/iterator"
)

// listLogs lists all available logs in the project.
func listLogs(w io.Writer, projectID string) error {
	ctx := context.Background()

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

	iter := client.Logs(ctx)
	for {
		log, err := iter.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return fmt.Errorf("List logs failed: %w", err)
		}
		fmt.Fprintf(w, "%s\n", log)
	}
	return nil
}

Java

Para saber como instalar e usar a biblioteca de cliente para o Logging, consulte Bibliotecas de cliente do Logging.

Para autenticar no Logging, configure o Application Default Credentials. Para mais informações, consulte Configurar a autenticação para um ambiente de desenvolvimento local.

Ver no GitHub (em inglês) Feedback
import com.google.api.gax.paging.Page;
import com.google.cloud.logging.Logging;
import com.google.cloud.logging.LoggingOptions;

public class ListLogs {

  public static void main(String... args) throws Exception {

    try (Logging logging = LoggingOptions.getDefaultInstance().getService()) {

      // List all log names
      Page<String> logNames = logging.listLogs();
      while (logNames != null) {
        for (String logName : logNames.iterateAll()) {
          System.out.println(logName);
        }
        logNames = logNames.getNextPage();
      }
    }
  }
}

Node.js

Para saber como instalar e usar a biblioteca de cliente para o Logging, consulte Bibliotecas de cliente do Logging.

Para autenticar no Logging, configure o Application Default Credentials. Para mais informações, consulte Configurar a autenticação para um ambiente de desenvolvimento local.

Ver no GitHub (em inglês) Feedback
// Imports the Google Cloud client library
const {Logging} = require('@google-cloud/logging');

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

async function printLogNames() {
  const [logs] = await logging.getLogs();
  console.log('Logs:');
  logs.forEach(log => {
    console.log(log.name);
  });
}
printLogNames();

Python

Para saber como instalar e usar a biblioteca de cliente para o Logging, consulte Bibliotecas de cliente do Logging.

Para autenticar no Logging, configure o Application Default Credentials. Para mais informações, consulte Configurar a autenticação para um ambiente de desenvolvimento local.

Ver no GitHub (em inglês) Feedback
from typing import List
from google.cloud import logging_v2

def list_logs(project_id: str) -> List[str]:
    """Lists all logs in a project.

    Args:
        project_id: the ID of the project

    Returns:
        A list of log names.
    """
    client = logging_v2.services.logging_service_v2.LoggingServiceV2Client()
    request = logging_v2.types.ListLogsRequest(
        parent=f"projects/{project_id}",
    )

    logs = client.list_logs(request=request)
    for log in logs:
        print(log)

    return logs

A seguir

Para pesquisar e filtrar amostras de código para outros produtos do Google Cloud, consulte o navegador de amostra do Google Cloud.