...

Source file src/cloud.google.com/go/logging/logadmin/examples_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  
    21  	"cloud.google.com/go/logging/logadmin"
    22  )
    23  
    24  func ExampleNewClient() {
    25  	ctx := context.Background()
    26  	client, err := logadmin.NewClient(ctx, "my-project")
    27  	if err != nil {
    28  		// TODO: Handle error.
    29  	}
    30  	// Use client to manage logs, metrics and sinks.
    31  	// Close the client when finished.
    32  	if err := client.Close(); err != nil {
    33  		// TODO: Handle error.
    34  	}
    35  }
    36  
    37  func ExampleClient_DeleteLog() {
    38  	ctx := context.Background()
    39  	client, err := logadmin.NewClient(ctx, "my-project")
    40  	if err != nil {
    41  		// TODO: Handle error.
    42  	}
    43  	err = client.DeleteLog(ctx, "my-log")
    44  	if err != nil {
    45  		// TODO: Handle error.
    46  	}
    47  }
    48  
    49  func ExampleClient_CreateMetric() {
    50  	ctx := context.Background()
    51  	client, err := logadmin.NewClient(ctx, "my-project")
    52  	if err != nil {
    53  		// TODO: Handle error.
    54  	}
    55  	err = client.CreateMetric(ctx, &logadmin.Metric{
    56  		ID:          "severe-errors",
    57  		Description: "entries at ERROR or higher severities",
    58  		Filter:      "severity >= ERROR",
    59  	})
    60  	if err != nil {
    61  		// TODO: Handle error.
    62  	}
    63  }
    64  
    65  func ExampleClient_DeleteMetric() {
    66  	ctx := context.Background()
    67  	client, err := logadmin.NewClient(ctx, "my-project")
    68  	if err != nil {
    69  		// TODO: Handle error.
    70  	}
    71  	if err := client.DeleteMetric(ctx, "severe-errors"); err != nil {
    72  		// TODO: Handle error.
    73  	}
    74  }
    75  
    76  func ExampleClient_Metric() {
    77  	ctx := context.Background()
    78  	client, err := logadmin.NewClient(ctx, "my-project")
    79  	if err != nil {
    80  		// TODO: Handle error.
    81  	}
    82  	m, err := client.Metric(ctx, "severe-errors")
    83  	if err != nil {
    84  		// TODO: Handle error.
    85  	}
    86  	fmt.Println(m)
    87  }
    88  
    89  func ExampleClient_UpdateMetric() {
    90  	ctx := context.Background()
    91  	client, err := logadmin.NewClient(ctx, "my-project")
    92  	if err != nil {
    93  		// TODO: Handle error.
    94  	}
    95  	err = client.UpdateMetric(ctx, &logadmin.Metric{
    96  		ID:          "severe-errors",
    97  		Description: "entries at high severities",
    98  		Filter:      "severity > ERROR",
    99  	})
   100  	if err != nil {
   101  		// TODO: Handle error.
   102  	}
   103  }
   104  
   105  func ExampleClient_CreateSink() {
   106  	ctx := context.Background()
   107  	client, err := logadmin.NewClient(ctx, "my-project")
   108  	if err != nil {
   109  		// TODO: Handle error.
   110  	}
   111  	sink, err := client.CreateSink(ctx, &logadmin.Sink{
   112  		ID:          "severe-errors-to-gcs",
   113  		Destination: "storage.googleapis.com/my-bucket",
   114  		Filter:      "severity >= ERROR",
   115  	})
   116  	if err != nil {
   117  		// TODO: Handle error.
   118  	}
   119  	fmt.Println(sink)
   120  }
   121  
   122  func ExampleClient_DeleteSink() {
   123  	ctx := context.Background()
   124  	client, err := logadmin.NewClient(ctx, "my-project")
   125  	if err != nil {
   126  		// TODO: Handle error.
   127  	}
   128  	if err := client.DeleteSink(ctx, "severe-errors-to-gcs"); err != nil {
   129  		// TODO: Handle error.
   130  	}
   131  }
   132  
   133  func ExampleClient_Sink() {
   134  	ctx := context.Background()
   135  	client, err := logadmin.NewClient(ctx, "my-project")
   136  	if err != nil {
   137  		// TODO: Handle error.
   138  	}
   139  	s, err := client.Sink(ctx, "severe-errors-to-gcs")
   140  	if err != nil {
   141  		// TODO: Handle error.
   142  	}
   143  	fmt.Println(s)
   144  }
   145  
   146  func ExampleClient_UpdateSink() {
   147  	ctx := context.Background()
   148  	client, err := logadmin.NewClient(ctx, "my-project")
   149  	if err != nil {
   150  		// TODO: Handle error.
   151  	}
   152  	sink, err := client.UpdateSink(ctx, &logadmin.Sink{
   153  		ID:          "severe-errors-to-gcs",
   154  		Destination: "storage.googleapis.com/my-other-bucket",
   155  		Filter:      "severity >= ERROR",
   156  	})
   157  	if err != nil {
   158  		// TODO: Handle error.
   159  	}
   160  	fmt.Println(sink)
   161  }
   162  

View as plain text