gogoogle-cloud-platformstackdrivergoogle-cloud-logginggoogle-cloud-error-reporting

How to log a error to Stackdriver Error Reporting via Stackdriver Logging


I am trying to log an error to Stackdriver Error Reporting in Go. On the first page of the Error Reporting, there is stated "Reporting errors from your application can be achieved by logging application errors to Google Stackdriver Logging or..." (https://cloud.google.com/error-reporting/docs/). How do I do that with the Go client libraries?

The Entry provided by the logging library is constructed like this:

github.com/GoogleCloudPlatform/.../logging.go#L412

type Entry struct {
    Timestamp time.Time
    Severity Severity
    Payload interface{}
    Labels map[string]string
    InsertID string
    HTTPRequest *HTTPRequest
    Operation *logpb.LogEntryOperation
    LogName string
    Resource *mrpb.MonitoredResource
}

Do I need to marshal this JSON structure into the Payload? Or can I insert the stacktrace as string?


Solution

  • There is a dedicated Go package that should help you achieve this: import "cloud.google.com/go/errorreporting"

    You can configure it to report errors via Stackdriver Logging, and it will take care of the sending the correct log structure.