Skip to content

Commit b6a2a4d

Browse files
committed
Add @type the the different error severeties
Because we want the errors to show up in the Google Cloud Error Reporting tool. This achieves that. We could also add `stack_trace` at the root of the logEntry but that is not always desired. Plus we have the source location.
1 parent 4cd3a39 commit b6a2a4d

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

main.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ const (
2929

3030
const maxSize = 102400
3131

32+
var errorMessageType = "type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent"
33+
3234
var stdout = bufio.NewWriter(os.Stdout)
3335

3436
type Logger struct{}
@@ -167,11 +169,18 @@ func (l *Logger) writeLog(severety severety, message string, obj interface{}) {
167169
return
168170
}
169171

172+
var messageType *string
173+
switch severety {
174+
case error_severety, critical_severety, alert_severety, emergency_severety:
175+
messageType = &errorMessageType
176+
}
177+
170178
payload := &stackdriverLogStruct{
171179
JsonPayload: obj,
172-
TextPayload: message,
180+
Message: message,
173181
Severity: severety,
174182
Timestamp: time.Now(),
183+
Type: messageType,
175184
SourceLocation: &sourceLocation{
176185
File: relative(file),
177186
Function: runtime.FuncForPC(pc).Name(),
@@ -199,11 +208,12 @@ func relative(path string) string {
199208

200209
// stackdriverLogStruct source https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry
201210
type stackdriverLogStruct struct {
202-
TextPayload string `json:"message,omitempty"`
211+
Message string `json:"message"`
203212
JsonPayload interface{} `json:"jsonPayload,omitempty"`
204213
Severity severety `json:"severity"`
205214
Timestamp time.Time `json:"timestamp"`
206-
SourceLocation *sourceLocation `json:"sourceLocation,omitempty"`
215+
SourceLocation *sourceLocation `json:"logging.googleapis.com/sourceLocation"`
216+
Type *string `json:"@type,omitempty"`
207217
}
208218
type sourceLocation struct {
209219
File string `json:"file"`

0 commit comments

Comments
 (0)