top

Package logging contains a Stackdriver Logging client suitable for writing logs. For reading logs, and working with sinks, metrics and monitored resources, see package cloud.google.com/go/logging/logadmin.

This client uses Logging API v2. See https://cloud.google.com/logging/docs/api/v2/ for an introduction to the API.

This package is experimental and subject to API changes.

Creating a Client

Use a Client to interact with the Stackdriver Logging API.

// Create a Client
ctx := context.Background()
client, err := logging.NewClient(ctx, "my-project")
if err != nil {
	// TODO: Handle error.
}

Basic Usage

For most use-cases, you'll want to add log entries to a buffer to be periodically flushed (automatically and asynchronously) to the Stackdriver Logging service.

// Initialize a logger
lg := client.Logger("my-log")

// Add entry to log buffer
lg.Log(logging.Entry{Payload: "something happened!"})

Closing your Client

You should call Client.Close before your program exits to flush any buffered log entries to the Stackdriver Logging service.

// Close the client when finished.
err = client.Close()
if err != nil {
	// TODO: Handle error.
}

Synchronous Logging

For critical errors, you may want to send your log entries immediately. LogSync is slow and will block until the log entry has been sent, so it is not recommended for basic use.

lg.LogSync(ctx, logging.Entry{Payload: "ALERT! Something critical happened!"})

The Standard Logger Interface

You may want use a standard log.Logger in your program.

// stdlg implements log.Logger
stdlg := lg.StandardLogger(logging.Info)
stdlg.Println("some info")

Log Levels

An Entry may have one of a number of severity levels associated with it.

logging.Entry{
	Payload: "something terrible happened!",
	Severity: logging.Critical,
}

Imported by 27 package(s)

  1. camlistore.org/pkg/deploy/gce
  2. camlistore.org/pkg/osutil/gce
  3. camlistore.org/server/camlistored
  4. camlistore.org/server/camnetdns
  5. camlistore.org/website
  6. cloud.google.com/go/errors
  7. cloud.google.com/go/logging/logadmin
  8. github.com/FiloSottile/camlistore/pkg/deploy/gce
  9. github.com/FiloSottile/camlistore/pkg/osutil/gce
  10. github.com/FiloSottile/camlistore/server/camlistored
  11. github.com/FiloSottile/camlistore/server/camnetdns
  12. github.com/FiloSottile/camlistore/website
  13. github.com/GoogleCloudPlatform/gcloud-golang/errors
  14. github.com/Nitecon/gcloud-golang/errors
  15. github.com/StratexIO/gclog
  16. github.com/antihax/gddo/gddo-server
  17. github.com/camlistore/camlistore/pkg/deploy/gce
  18. github.com/camlistore/camlistore/pkg/osutil/gce
  19. github.com/camlistore/camlistore/server/camlistored
  20. github.com/golang/gddo/gddo-server
  21. github.com/jba/google-cloud-go/errors
  22. github.com/jmhodges/howsmyssl
  23. github.com/mpl/camlistore/server/camlistored
  24. github.com/mpl/camlistore/server/camnetdns
  25. github.com/nathany/gddo/gddo-server
  26. github.com/stratexio/gclog
  27. github.com/tortuoise/gcloud-golang/errors

Imported only in test by 1 package(s)

  1. github.com/GoogleCloudPlatform/gcloud-golang/logging

Imports 12 package(s)

  1. cloud.google.com/go/logging/apiv2
  2. github.com/golang/protobuf/proto
  3. google.golang.org/api/support/bundler
  4. google.golang.org/genproto/googleapis/api/monitoredres
  5. google.golang.org/genproto/googleapis/logging/v2
  6. github.com/golang/protobuf/ptypes/timestamp
  7. google.golang.org/api/option
  8. github.com/golang/protobuf/ptypes
  9. github.com/golang/protobuf/ptypes/struct
  10. golang.org/x/net/context
  11. google.golang.org/genproto/googleapis/logging/type
  12. cloud.google.com/go/logging/internal

Test imports 7 package(s)

  1. github.com/golang/protobuf/ptypes/duration
  2. cloud.google.com/go/logging/internal/testing
  3. google.golang.org/grpc
  4. cloud.google.com/go/internal/testutil
  5. cloud.google.com/go/logging/logadmin
  6. golang.org/x/oauth2
  7. google.golang.org/api/iterator