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.
 
 
 

135 lines
4.1 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. /*
  15. Package logging contains a Stackdriver Logging client suitable for writing logs.
  16. For reading logs, and working with sinks, metrics and monitored resources,
  17. see package cloud.google.com/go/logging/logadmin.
  18. This client uses Logging API v2.
  19. See https://cloud.google.com/logging/docs/api/v2/ for an introduction to the API.
  20. Creating a Client
  21. Use a Client to interact with the Stackdriver Logging API.
  22. // Create a Client
  23. ctx := context.Background()
  24. client, err := logging.NewClient(ctx, "my-project")
  25. if err != nil {
  26. // TODO: Handle error.
  27. }
  28. Basic Usage
  29. For most use cases, you'll want to add log entries to a buffer to be periodically
  30. flushed (automatically and asynchronously) to the Stackdriver Logging service.
  31. // Initialize a logger
  32. lg := client.Logger("my-log")
  33. // Add entry to log buffer
  34. lg.Log(logging.Entry{Payload: "something happened!"})
  35. Closing your Client
  36. You should call Client.Close before your program exits to flush any buffered log entries to the Stackdriver Logging service.
  37. // Close the client when finished.
  38. err = client.Close()
  39. if err != nil {
  40. // TODO: Handle error.
  41. }
  42. Synchronous Logging
  43. For critical errors, you may want to send your log entries immediately.
  44. LogSync is slow and will block until the log entry has been sent, so it is
  45. not recommended for normal use.
  46. err = lg.LogSync(ctx, logging.Entry{Payload: "ALERT! Something critical happened!"})
  47. if err != nil {
  48. // TODO: Handle error.
  49. }
  50. Payloads
  51. An entry payload can be a string, as in the examples above. It can also be any value
  52. that can be marshaled to a JSON object, like a map[string]interface{} or a struct:
  53. type MyEntry struct {
  54. Name string
  55. Count int
  56. }
  57. lg.Log(logging.Entry{Payload: MyEntry{Name: "Bob", Count: 3}})
  58. If you have a []byte of JSON, wrap it in json.RawMessage:
  59. j := []byte(`{"Name": "Bob", "Count": 3}`)
  60. lg.Log(logging.Entry{Payload: json.RawMessage(j)})
  61. The Standard Logger Interface
  62. You may want use a standard log.Logger in your program.
  63. // stdlg implements log.Logger
  64. stdlg := lg.StandardLogger(logging.Info)
  65. stdlg.Println("some info")
  66. Log Levels
  67. An Entry may have one of a number of severity levels associated with it.
  68. logging.Entry{
  69. Payload: "something terrible happened!",
  70. Severity: logging.Critical,
  71. }
  72. Viewing Logs
  73. You can view Stackdriver logs for projects at
  74. https://console.cloud.google.com/logs/viewer. Use the dropdown at the top left. When
  75. running from a Google Cloud Platform VM, select "GCE VM Instance". Otherwise, select
  76. "Google Project" and then the project ID. Logs for organizations, folders and billing
  77. accounts can be viewed on the command line with the "gcloud logging read" command.
  78. Grouping Logs by Request
  79. To group all the log entries written during a single HTTP request, create two
  80. Loggers, a "parent" and a "child," with different log IDs. Both should be in the same
  81. project, and have the same MonitoredResouce type and labels.
  82. - Parent entries must have HTTPRequest.Request populated. (Strictly speaking, only the URL is necessary.)
  83. - A child entry's timestamp must be within the time interval covered by the parent request (i.e., older
  84. than parent.Timestamp, and newer than parent.Timestamp - parent.HTTPRequest.Latency, assuming the
  85. parent timestamp marks the end of the request.
  86. - The trace field must be populated in all of the entries and match exactly.
  87. You should observe the child log entries grouped under the parent on the console. The
  88. parent entry will not inherit the severity of its children; you must update the
  89. parent severity yourself.
  90. */
  91. package logging // import "cloud.google.com/go/logging"