如何通过堆栈驱动程序日志记录将错误记录到Stackdriver错误报告

时间:2017-01-16 20:07:23

标签: go google-cloud-platform stackdriver google-cloud-logging google-cloud-error-reporting

我正在尝试将错误记录到Go中的Stackdriver错误报告。在错误报告的第一页上,声明“通过将应用程序错误记录到Google Stackdriver Logging或......”(https://cloud.google.com/error-reporting/docs/),可以实现“应用程序报告错误”。如何使用Go客户端库?

日志记录库提供的Entry构造如下:

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
}

我是否需要将this JSON结构编组到Payload中?或者我可以将stacktrace作为字符串插入吗?

2 个答案:

答案 0 :(得分:2)

a dedicated Go package可以帮助您实现这一目标。 import "cloud.google.com/go/errors"

您可以将其配置为通过Stackdriver Logging报告错误,它将负责发送正确的日志结构。

答案 1 :(得分:1)

来自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).

看起来将stacktrace作为字符串插入是可行的方法。