简体   繁体   中英

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?

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.

From the docs :

// Payload must be either a string or something that
// marshals via the encoding/json package to a JSON object
// (and not any other type of JSON value).

Looks like inserting stacktrace as a string is the way to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM