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

huangapple go评论92阅读模式
英文:

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

问题

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

logging库提供的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
}

我需要将这个 JSON结构编组为Payload吗?还是可以将堆栈跟踪插入为字符串?

英文:

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?

答案1

得分: 2

有一个专门的Go包可以帮助你实现这个功能:import "cloud.google.com/go/errorreporting"

你可以配置它通过Stackdriver Logging来报告错误,它会负责发送正确的日志结构。

英文:

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.

答案2

得分: 1

根据文档

// 负载必须是一个字符串,或者是通过 encoding/json 包编组为 JSON 对象的内容
// (而不是其他类型的 JSON 值)。

看起来将堆栈跟踪作为字符串插入是可行的方式。

英文:

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.

huangapple
  • 本文由 发表于 2017年1月17日 04:07:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/41684218.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定