2022-03-10 16:26:02 -05:00
|
|
|
## Cloud Logging [![Go Reference](https://pkg.go.dev/badge/cloud.google.com/go/logging.svg)](https://pkg.go.dev/cloud.google.com/go/logging)
|
2019-08-26 09:47:02 -04:00
|
|
|
|
2022-03-10 16:26:02 -05:00
|
|
|
- [About Cloud Logging](https://cloud.google.com/logging/)
|
2019-08-26 09:47:02 -04:00
|
|
|
- [API documentation](https://cloud.google.com/logging/docs)
|
2022-03-10 16:26:02 -05:00
|
|
|
- [Go client documentation](https://pkg.go.dev/cloud.google.com/go/logging)
|
2019-08-26 09:47:02 -04:00
|
|
|
- [Complete sample programs](https://github.com/GoogleCloudPlatform/golang-samples/tree/master/logging)
|
|
|
|
|
|
|
|
### Example Usage
|
|
|
|
|
|
|
|
First create a `logging.Client` to use throughout your application:
|
|
|
|
[snip]:# (logging-1)
|
2022-03-10 16:26:02 -05:00
|
|
|
|
2019-08-26 09:47:02 -04:00
|
|
|
```go
|
|
|
|
ctx := context.Background()
|
|
|
|
client, err := logging.NewClient(ctx, "my-project")
|
|
|
|
if err != nil {
|
2022-03-10 16:26:02 -05:00
|
|
|
// TODO: Handle error.
|
2019-08-26 09:47:02 -04:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Usually, you'll want to add log entries to a buffer to be periodically flushed
|
2022-03-10 16:26:02 -05:00
|
|
|
(automatically and asynchronously) to the Cloud Logging service.
|
2019-08-26 09:47:02 -04:00
|
|
|
[snip]:# (logging-2)
|
2022-03-10 16:26:02 -05:00
|
|
|
|
2019-08-26 09:47:02 -04:00
|
|
|
```go
|
|
|
|
logger := client.Logger("my-log")
|
|
|
|
logger.Log(logging.Entry{Payload: "something happened!"})
|
|
|
|
```
|
|
|
|
|
|
|
|
Close your client before your program exits, to flush any buffered log entries.
|
|
|
|
[snip]:# (logging-3)
|
2022-03-10 16:26:02 -05:00
|
|
|
|
2019-08-26 09:47:02 -04:00
|
|
|
```go
|
|
|
|
err = client.Close()
|
|
|
|
if err != nil {
|
2022-03-10 16:26:02 -05:00
|
|
|
// TODO: Handle error.
|
2019-08-26 09:47:02 -04:00
|
|
|
}
|
2022-03-10 16:26:02 -05:00
|
|
|
```
|