-
Notifications
You must be signed in to change notification settings - Fork 45
/
report.go
70 lines (56 loc) · 1.77 KB
/
report.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package zapdriver
import (
"runtime"
"strconv"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const contextKey = "context"
// ErrorReport adds the correct Stackdriver "context" field for getting the log line
// reported as error.
//
// see: https://cloud.google.com/error-reporting/docs/formatting-error-messages
func ErrorReport(pc uintptr, file string, line int, ok bool) zap.Field {
return zap.Object(contextKey, newReportContext(pc, file, line, ok))
}
// reportLocation is the source code location information associated with the log entry
// for the purpose of reporting an error,
// if any.
type reportLocation struct {
File string `json:"filePath"`
Line string `json:"lineNumber"`
Function string `json:"functionName"`
}
// MarshalLogObject implements zapcore.ObjectMarshaller interface.
func (location reportLocation) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddString("filePath", location.File)
enc.AddString("lineNumber", location.Line)
enc.AddString("functionName", location.Function)
return nil
}
// reportContext is the context information attached to a log for reporting errors
type reportContext struct {
ReportLocation reportLocation `json:"reportLocation"`
}
// MarshalLogObject implements zapcore.ObjectMarshaller interface.
func (context reportContext) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddObject("reportLocation", context.ReportLocation)
return nil
}
func newReportContext(pc uintptr, file string, line int, ok bool) *reportContext {
if !ok {
return nil
}
var function string
if fn := runtime.FuncForPC(pc); fn != nil {
function = fn.Name()
}
context := &reportContext{
ReportLocation: reportLocation{
File: file,
Line: strconv.Itoa(line),
Function: function,
},
}
return context
}