forked from GoogleCloudPlatform/golang-samples
/
main.go
51 lines (38 loc) · 1.16 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright 2016 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
// [START logging_quickstart]
// Sample logging-quickstart writes a log entry to Stackdriver Logging.
package main
import (
"fmt"
"log"
// Imports the Stackdriver Logging client package.
"cloud.google.com/go/logging"
"golang.org/x/net/context"
)
func main() {
ctx := context.Background()
// Sets your Google Cloud Platform project ID.
projectID := "YOUR_PROJECT_ID"
// Creates a client.
client, err := logging.NewClient(ctx, projectID)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
// Sets the name of the log to write to.
logName := "my-log"
// Selects the log to write to.
logger := client.Logger(logName)
// Sets the data to log.
text := "Hello, world!"
// Adds an entry to the log buffer.
logger.Log(logging.Entry{Payload: text})
// Closes the client and flushes the buffer to the Stackdriver Logging
// service.
if err := client.Close(); err != nil {
log.Fatalf("Failed to close client: %v", err)
}
fmt.Printf("Logged: %v\n", text)
}
// [END logging_quickstart]