-
Notifications
You must be signed in to change notification settings - Fork 45
/
source.go
66 lines (53 loc) · 1.74 KB
/
source.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
package zapdriver
import (
"runtime"
"strconv"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const sourceKey = "logging.googleapis.com/sourceLocation"
// SourceLocation adds the correct Stackdriver "SourceLocation" field.
//
// see: https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#LogEntrySourceLocation
func SourceLocation(pc uintptr, file string, line int, ok bool) zap.Field {
return zap.Object(sourceKey, newSource(pc, file, line, ok))
}
// source is the source code location information associated with the log entry,
// if any.
type source struct {
// Optional. Source file name. Depending on the runtime environment, this
// might be a simple name or a fully-qualified name.
File string `json:"file"`
// Optional. Line within the source file. 1-based; 0 indicates no line number
// available.
Line string `json:"line"`
// Optional. Human-readable name of the function or method being invoked, with
// optional context such as the class or package name. This information may be
// used in contexts such as the logs viewer, where a file and line number are
// less meaningful.
//
// The format should be dir/package.func.
Function string `json:"function"`
}
// MarshalLogObject implements zapcore.ObjectMarshaller interface.
func (source source) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddString("file", source.File)
enc.AddString("line", source.Line)
enc.AddString("function", source.Function)
return nil
}
func newSource(pc uintptr, file string, line int, ok bool) *source {
if !ok {
return nil
}
var function string
if fn := runtime.FuncForPC(pc); fn != nil {
function = fn.Name()
}
source := &source{
File: file,
Line: strconv.Itoa(line),
Function: function,
}
return source
}