...

Source file src/cloud.google.com/go/logging/logadmin/example_entry_iterator_test.go

Documentation: cloud.google.com/go/logging/logadmin

     1  // Copyright 2016 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package logadmin_test
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"time"
    21  
    22  	"cloud.google.com/go/logging/logadmin"
    23  	"google.golang.org/api/iterator"
    24  )
    25  
    26  func ExampleClient_Entries() {
    27  	ctx := context.Background()
    28  	client, err := logadmin.NewClient(ctx, "my-project")
    29  	if err != nil {
    30  		// TODO: Handle error.
    31  	}
    32  	it := client.Entries(ctx, logadmin.Filter(`logName = "projects/my-project/logs/my-log"`))
    33  	_ = it // TODO: iterate using Next or iterator.Pager.
    34  }
    35  
    36  func ExampleFilter_timestamp() {
    37  	// This example demonstrates how to list the last 24 hours of log entries.
    38  	ctx := context.Background()
    39  	client, err := logadmin.NewClient(ctx, "my-project")
    40  	if err != nil {
    41  		// TODO: Handle error.
    42  	}
    43  	oneDayAgo := time.Now().Add(-24 * time.Hour)
    44  	t := oneDayAgo.Format(time.RFC3339) // Logging API wants timestamps in RFC 3339 format.
    45  	it := client.Entries(ctx, logadmin.Filter(fmt.Sprintf(`timestamp > "%s"`, t)))
    46  	_ = it // TODO: iterate using Next or iterator.Pager.
    47  }
    48  
    49  func ExampleEntryIterator_Next() {
    50  	ctx := context.Background()
    51  	client, err := logadmin.NewClient(ctx, "my-project")
    52  	if err != nil {
    53  		// TODO: Handle error.
    54  	}
    55  	it := client.Entries(ctx)
    56  	for {
    57  		entry, err := it.Next()
    58  		if err == iterator.Done {
    59  			break
    60  		}
    61  		if err != nil {
    62  			// TODO: Handle error.
    63  		}
    64  		fmt.Println(entry)
    65  	}
    66  }
    67  

View as plain text