doc.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2016 Google Inc. All Rights Reserved.
  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. This package is experimental and subject to API changes.
  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 basic use.
  47. lg.LogSync(ctx, logging.Entry{Payload: "ALERT! Something critical happened!"})
  48. The Standard Logger Interface
  49. You may want use a standard log.Logger in your program.
  50. // stdlg implements log.Logger
  51. stdlg := lg.StandardLogger(logging.Info)
  52. stdlg.Println("some info")
  53. Log Levels
  54. An Entry may have one of a number of severity levels associated with it.
  55. logging.Entry{
  56. Payload: "something terrible happened!",
  57. Severity: logging.Critical,
  58. }
  59. */
  60. package logging // import "cloud.google.com/go/logging"