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.
 
 
 

118 lines
3.3 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. Note: This package is in beta. Some backwards-incompatible changes may occur.
  21. Creating a Client
  22. Use a Client to interact with the Stackdriver Logging API.
  23. // Create a Client
  24. ctx := context.Background()
  25. client, err := logging.NewClient(ctx, "my-project")
  26. if err != nil {
  27. // TODO: Handle error.
  28. }
  29. Basic Usage
  30. For most use cases, you'll want to add log entries to a buffer to be periodically
  31. flushed (automatically and asynchronously) to the Stackdriver Logging service.
  32. // Initialize a logger
  33. lg := client.Logger("my-log")
  34. // Add entry to log buffer
  35. lg.Log(logging.Entry{Payload: "something happened!"})
  36. Closing your Client
  37. You should call Client.Close before your program exits to flush any buffered log entries to the Stackdriver Logging service.
  38. // Close the client when finished.
  39. err = client.Close()
  40. if err != nil {
  41. // TODO: Handle error.
  42. }
  43. Synchronous Logging
  44. For critical errors, you may want to send your log entries immediately.
  45. LogSync is slow and will block until the log entry has been sent, so it is
  46. not recommended for normal use.
  47. lg.LogSync(ctx, logging.Entry{Payload: "ALERT! Something critical happened!"})
  48. Payloads
  49. An entry payload can be a string, as in the examples above. It can also be any value
  50. that can be marshaled to a JSON object, like a map[string]interface{} or a struct:
  51. type MyEntry struct {
  52. Name string
  53. Count int
  54. }
  55. lg.Log(logging.Entry{Payload: MyEntry{Name: "Bob", Count: 3}})
  56. If you have a []byte of JSON, wrap it in json.RawMessage:
  57. j := []byte(`{"Name": "Bob", "Count": 3}`)
  58. lg.Log(logging.Entry{Payload: json.RawMessage(j)})
  59. The Standard Logger Interface
  60. You may want use a standard log.Logger in your program.
  61. // stdlg implements log.Logger
  62. stdlg := lg.StandardLogger(logging.Info)
  63. stdlg.Println("some info")
  64. Log Levels
  65. An Entry may have one of a number of severity levels associated with it.
  66. logging.Entry{
  67. Payload: "something terrible happened!",
  68. Severity: logging.Critical,
  69. }
  70. Viewing Logs
  71. You can view Stackdriver logs for projects at
  72. https://console.cloud.google.com/logs/viewer. Use the dropdown at the top left. When
  73. running from a Google Cloud Platform VM, select "GCE VM Instance". Otherwise, select
  74. "Google Project" and then the project ID. Logs for organizations, folders and billing
  75. accounts can be viewed on the command line with the "gcloud logging read" command.
  76. */
  77. package logging // import "cloud.google.com/go/logging"