-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzap_option.go
33 lines (28 loc) · 1.12 KB
/
zap_option.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package cloudlog
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type serviceContext struct {
Service string
}
// MarshalLogObject add value in object.
// see: https://pkg.go.dev/go.uber.org/zap#Object
func (s serviceContext) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddString("service", s.Service)
return nil
}
// GetCloudServiceContextOption get zap.Option about serviceName.
// You have to call this function when you use Error Reporting.
// e.g. cloud-run-backend-api
func GetCloudServiceContextOption(serviceName string) zap.Option {
return zap.Fields(zap.Object("serviceContext", serviceContext{Service: serviceName}))
}
// AddCloudErrorReportingOption add zap option about Error Reporting.
// You can use Error Reporting in GCP when you use this function.
// see: https://cloud.google.com/error-reporting/docs/formatting-error-messages?hl=ja#json_representation
func AddCloudErrorReportingOption(zapLogger *zap.Logger) *zap.Logger {
typeKey := "@type"
typeValue := "type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent"
return zapLogger.With(zap.String(typeKey, typeValue))
}