...

Source file src/cloud.google.com/go/logging/logadmin/example_paging_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  	"bytes"
    19  	"context"
    20  	"flag"
    21  	"fmt"
    22  	"html/template"
    23  	"log"
    24  	"net/http"
    25  	"time"
    26  
    27  	"cloud.google.com/go/logging"
    28  	"cloud.google.com/go/logging/logadmin"
    29  	"google.golang.org/api/iterator"
    30  )
    31  
    32  var (
    33  	client    *logadmin.Client
    34  	projectID = flag.String("project-id", "", "ID of the project to use")
    35  	filter    string
    36  )
    37  
    38  func ExampleClient_Entries_pagination() {
    39  	// This example demonstrates how to iterate through items a page at a time
    40  	// even if each successive page is fetched by a different process. It is a
    41  	// complete web server that displays pages of log entries. To run it as a
    42  	// standalone program, rename both the package and this function to "main".
    43  	ctx := context.Background()
    44  	flag.Parse()
    45  	if *projectID == "" {
    46  		log.Fatal("-project-id missing")
    47  	}
    48  	var err error
    49  	client, err = logadmin.NewClient(ctx, *projectID)
    50  	if err != nil {
    51  		log.Fatalf("creating logging client: %v", err)
    52  	}
    53  
    54  	// Filter for logs of a specific name.
    55  	logName := fmt.Sprintf(`logName = "projects/%s/logs/testlog"`, *projectID)
    56  
    57  	// Filter for logs from the last twenty-four hours.
    58  	yesterday := time.Now().Add(-24 * time.Hour).UTC()
    59  	dayAgo := fmt.Sprintf("timestamp >= %q", yesterday.Format(time.RFC3339))
    60  
    61  	filter = fmt.Sprintf("%s AND %s", logName, dayAgo)
    62  
    63  	http.HandleFunc("/entries", handleEntries)
    64  	log.Print("listening on 8080")
    65  	log.Fatal(http.ListenAndServe(":8080", nil))
    66  }
    67  
    68  var pageTemplate = template.Must(template.New("").Parse(`
    69  <table>
    70    {{range .Entries}}
    71      <tr><td>{{.}}</td></tr>
    72    {{end}}
    73  </table>
    74  {{if .Next}}
    75    <a href="/entries?pageToken={{.Next}}">Next Page</a>
    76  {{end}}
    77  `))
    78  
    79  func handleEntries(w http.ResponseWriter, r *http.Request) {
    80  	ctx := context.Background()
    81  	it := client.Entries(ctx, logadmin.Filter(filter))
    82  	var entries []*logging.Entry
    83  	nextTok, err := iterator.NewPager(it, 5, r.URL.Query().Get("pageToken")).NextPage(&entries)
    84  	if err != nil {
    85  		http.Error(w, fmt.Sprintf("problem getting the next page: %v", err), http.StatusInternalServerError)
    86  		return
    87  	}
    88  	data := struct {
    89  		Entries []*logging.Entry
    90  		Next    string
    91  	}{
    92  		entries,
    93  		nextTok,
    94  	}
    95  	var buf bytes.Buffer
    96  	if err := pageTemplate.Execute(&buf, data); err != nil {
    97  		http.Error(w, fmt.Sprintf("problem executing page template: %v", err), http.StatusInternalServerError)
    98  	}
    99  	if _, err := buf.WriteTo(w); err != nil {
   100  		log.Printf("writing response: %v", err)
   101  	}
   102  }
   103  

View as plain text