로그 항목 표시

로그 항목을 나열하는 방법을 보여줍니다.

더 살펴보기

이 코드 샘플이 포함된 자세한 문서는 다음을 참조하세요.

코드 샘플

C#

Logging용 클라이언트 라이브러리를 설치하고 사용하는 방법은 Logging 클라이언트 라이브러리를 참조하세요.

Logging에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

private void ListLogEntries(string logId) {     var client = LoggingServiceV2Client.Create();     LogName logName = new LogName(s_projectId, logId);     ProjectName projectName = new ProjectName(s_projectId);     var results = client.ListLogEntries(Enumerable.Repeat(projectName, 1), $"logName={logName.ToString()}",         "timestamp desc", callSettings: _retryAWhile);     foreach (var row in results)     {         Console.WriteLine($"{row.TextPayload.Trim()}");     } } 

Go

Logging용 클라이언트 라이브러리를 설치하고 사용하는 방법은 Logging 클라이언트 라이브러리를 참조하세요.

Logging에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

import ( 	"context" 	"fmt" 	"log" 	"time"  	"cloud.google.com/go/logging" 	"cloud.google.com/go/logging/logadmin" 	"google.golang.org/api/iterator" )  func getEntries(projectID string) ([]*logging.Entry, error) { 	ctx := context.Background() 	adminClient, err := logadmin.NewClient(ctx, projectID) 	if err != nil { 		log.Fatalf("Failed to create logadmin client: %v", err) 	} 	defer adminClient.Close()  	var entries []*logging.Entry 	const name = "log-example" 	lastHour := time.Now().Add(-1 * time.Hour).Format(time.RFC3339)  	iter := adminClient.Entries(ctx, 		// Only get entries from the "log-example" log within the last hour. 		logadmin.Filter(fmt.Sprintf(`logName = "projects/%s/logs/%s" AND timestamp > "%s"`, projectID, name, lastHour)), 		// Get most recent entries first. 		logadmin.NewestFirst(), 	)  	// Fetch the most recent 20 entries. 	for len(entries) < 20 { 		entry, err := iter.Next() 		if err == iterator.Done { 			return entries, nil 		} 		if err != nil { 			return nil, err 		} 		entries = append(entries, entry) 	} 	return entries, nil } 

Java

Logging용 클라이언트 라이브러리를 설치하고 사용하는 방법은 Logging 클라이언트 라이브러리를 참조하세요.

Logging에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

import com.google.api.gax.paging.Page; import com.google.cloud.logging.LogEntry; import com.google.cloud.logging.Logging; import com.google.cloud.logging.Logging.EntryListOption; import com.google.cloud.logging.LoggingOptions; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.TimeZone;  public class ListLogEntries {    public static void main(String[] args) throws Exception {     // TODO(developer): Replace the variable value with valid log name before running the sample     // or provide it as an argument.     String logName = args.length > 0 ? args[0] : "test-log";      try (Logging logging = LoggingOptions.getDefaultInstance().getService()) {        // When composing a filter, using indexed fields, such as timestamp, resource.type, logName       // and       // others can help accelerate the results       // Full list of indexed fields here:       // https://cloud.google.com/logging/docs/view/advanced-queries#finding-quickly       // This sample restrict the results to only last minute to minimize number of API calls       Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));       calendar.add(Calendar.MINUTE, -1);       DateFormat rfc3339 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");       String logFilter =           "logName=projects/"               + logging.getOptions().getProjectId()               + "/logs/"               + logName               + " AND timestamp>=\""               + rfc3339.format(calendar.getTime())               + "\"";        // List all log entries       Page<LogEntry> entries = logging.listLogEntries(EntryListOption.filter(logFilter));       while (entries != null) {         for (LogEntry logEntry : entries.iterateAll()) {           System.out.println(logEntry);         }         entries = entries.getNextPage();       }     }   } }

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 logName = 'Name of the log from which to list entries, e.g. my-log';  const log = logging.log(logName);  async function printEntryMetadata() {   // List the most recent entries for a given log   // See https://googleapis.dev/nodejs/logging/latest/Logging.html#getEntries   const [entries] = await log.getEntries();   console.log('Logs:');   entries.forEach(entry => {     const metadata = entry.metadata;     console.log(`${metadata.timestamp}:`, metadata[metadata.payload]);   }); } printEntryMetadata();

PHP

Logging용 클라이언트 라이브러리를 설치하고 사용하는 방법은 Logging 클라이언트 라이브러리를 참조하세요.

Logging에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

use Google\Cloud\Logging\LoggingClient;  /**  * Print the timestamp and entry for the project and logger.  *  * @param string $projectId The Google project ID.  * @param string $loggerName The name of the logger.  */ function list_entries($projectId, $loggerName) {     $logging = new LoggingClient(['projectId' => $projectId]);     $loggerFullName = sprintf('projects/%s/logs/%s', $projectId, $loggerName);     $oneDayAgo = date(\DateTime::RFC3339, strtotime('-24 hours'));     $filter = sprintf(         'logName = "%s" AND timestamp >= "%s"',         $loggerFullName,         $oneDayAgo     );     $options = [         'filter' => $filter,     ];     $entries = $logging->entries($options);      // Print the entries     foreach ($entries as $entry) {         /* @var $entry \Google\Cloud\Logging\Entry */         $entryInfo = $entry->info();         if (isset($entryInfo['textPayload'])) {             $entryText = $entryInfo['textPayload'];         } else {             $entryPayload = [];             foreach ($entryInfo['jsonPayload'] as $key => $value) {                 $entryPayload[] = "$key: $value";             }             $entryText = '{' . implode(', ', $entryPayload) . '}';         }         printf('%s : %s' . PHP_EOL, $entryInfo['timestamp'], $entryText);     } }

Python

Logging용 클라이언트 라이브러리를 설치하고 사용하는 방법은 Logging 클라이언트 라이브러리를 참조하세요.

Logging에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

def list_entries(logger_name):     """Lists the most recent entries for a given logger."""     logging_client = logging.Client()     logger = logging_client.logger(logger_name)      print("Listing entries for logger {}:".format(logger.name))      for entry in logger.list_entries():         timestamp = entry.timestamp.isoformat()         print("* {}: {}".format(timestamp, entry.payload))  

Ruby

Logging용 클라이언트 라이브러리를 설치하고 사용하는 방법은 Logging 클라이언트 라이브러리를 참조하세요.

Logging에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

require "google/cloud/logging"  # log_name = "my_log_name" logging = Google::Cloud::Logging.new entries = logging.entries filter: "logName:#{log_name}",                           max:    1000,                           order:  "timestamp desc"  entries.each do |entry|   puts "[#{entry.timestamp}] #{entry.log_name} #{entry.payload.inspect}" end

다음 단계

다른 Google Cloud 제품의 코드 샘플을 검색하고 필터링하려면 Google Cloud 샘플 브라우저 참조하기