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.
 
 
 

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