You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

167 line
4.4 KiB

  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. package logging_test
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "os"
  19. "cloud.google.com/go/logging"
  20. "golang.org/x/net/context"
  21. )
  22. func ExampleNewClient() {
  23. ctx := context.Background()
  24. client, err := logging.NewClient(ctx, "my-project")
  25. if err != nil {
  26. // TODO: Handle error.
  27. }
  28. // Use client to manage logs, metrics and sinks.
  29. // Close the client when finished.
  30. if err := client.Close(); err != nil {
  31. // TODO: Handle error.
  32. }
  33. }
  34. func ExampleClient_Ping() {
  35. ctx := context.Background()
  36. client, err := logging.NewClient(ctx, "my-project")
  37. if err != nil {
  38. // TODO: Handle error.
  39. }
  40. if err := client.Ping(ctx); err != nil {
  41. // TODO: Handle error.
  42. }
  43. }
  44. // Although Logger.Flush and Client.Close both return errors, they don't tell you
  45. // whether the errors were frequent or significant. For most programs, it doesn't
  46. // matter if there were a few errors while writing logs, although if those few errors
  47. // indicated a bug in your program, you might want to know about them. The best way
  48. // to handle errors is by setting the OnError function. If it runs quickly, it will
  49. // see every error generated during logging.
  50. func ExampleNewClient_errorFunc() {
  51. ctx := context.Background()
  52. client, err := logging.NewClient(ctx, "my-project")
  53. if err != nil {
  54. // TODO: Handle error.
  55. }
  56. // Print all errors to stdout, and count them. Multiple calls to the OnError
  57. // function never happen concurrently, so there is no need for locking nErrs,
  58. // provided you don't read it until after the logging client is closed.
  59. var nErrs int
  60. client.OnError = func(e error) {
  61. fmt.Fprintf(os.Stdout, "logging: %v", e)
  62. nErrs++
  63. }
  64. // Use client to manage logs, metrics and sinks.
  65. // Close the client when finished.
  66. if err := client.Close(); err != nil {
  67. // TODO: Handle error.
  68. }
  69. fmt.Printf("saw %d errors\n", nErrs)
  70. }
  71. func ExampleClient_Logger() {
  72. ctx := context.Background()
  73. client, err := logging.NewClient(ctx, "my-project")
  74. if err != nil {
  75. // TODO: Handle error.
  76. }
  77. lg := client.Logger("my-log")
  78. _ = lg // TODO: use the Logger.
  79. }
  80. func ExampleLogger_LogSync() {
  81. ctx := context.Background()
  82. client, err := logging.NewClient(ctx, "my-project")
  83. if err != nil {
  84. // TODO: Handle error.
  85. }
  86. lg := client.Logger("my-log")
  87. err = lg.LogSync(ctx, logging.Entry{Payload: "red alert"})
  88. if err != nil {
  89. // TODO: Handle error.
  90. }
  91. }
  92. func ExampleLogger_Log() {
  93. ctx := context.Background()
  94. client, err := logging.NewClient(ctx, "my-project")
  95. if err != nil {
  96. // TODO: Handle error.
  97. }
  98. lg := client.Logger("my-log")
  99. lg.Log(logging.Entry{Payload: "something happened"})
  100. }
  101. // An Entry payload can be anything that marshals to a
  102. // JSON object, like a struct.
  103. func ExampleLogger_Log_struct() {
  104. type MyEntry struct {
  105. Name string
  106. Count int
  107. }
  108. ctx := context.Background()
  109. client, err := logging.NewClient(ctx, "my-project")
  110. if err != nil {
  111. // TODO: Handle error.
  112. }
  113. lg := client.Logger("my-log")
  114. lg.Log(logging.Entry{Payload: MyEntry{Name: "Bob", Count: 3}})
  115. }
  116. // To log a JSON value, wrap it in json.RawMessage.
  117. func ExampleLogger_Log_json() {
  118. ctx := context.Background()
  119. client, err := logging.NewClient(ctx, "my-project")
  120. if err != nil {
  121. // TODO: Handle error.
  122. }
  123. lg := client.Logger("my-log")
  124. j := []byte(`{"Name": "Bob", "Count": 3}`)
  125. lg.Log(logging.Entry{Payload: json.RawMessage(j)})
  126. }
  127. func ExampleLogger_Flush() {
  128. ctx := context.Background()
  129. client, err := logging.NewClient(ctx, "my-project")
  130. if err != nil {
  131. // TODO: Handle error.
  132. }
  133. lg := client.Logger("my-log")
  134. lg.Log(logging.Entry{Payload: "something happened"})
  135. lg.Flush()
  136. }
  137. func ExampleLogger_StandardLogger() {
  138. ctx := context.Background()
  139. client, err := logging.NewClient(ctx, "my-project")
  140. if err != nil {
  141. // TODO: Handle error.
  142. }
  143. lg := client.Logger("my-log")
  144. slg := lg.StandardLogger(logging.Info)
  145. slg.Println("an informative message")
  146. }
  147. func ExampleParseSeverity() {
  148. sev := logging.ParseSeverity("ALERT")
  149. fmt.Println(sev)
  150. // Output: Alert
  151. }