forked from tommy351/zap-stackdriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.go
149 lines (117 loc) · 3.21 KB
/
core.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package stackdriver
import (
"runtime"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
logKeyServiceContext = "serviceContext"
logKeyContextHTTPRequest = "context.httpRequest"
logKeyContextUser = "context.user"
logKeyContextReportLocation = "context.reportLocation"
)
var logLevelSeverity = map[zapcore.Level]string{
zapcore.DebugLevel: "DEBUG",
zapcore.InfoLevel: "INFO",
zapcore.WarnLevel: "WARNING",
zapcore.ErrorLevel: "ERROR",
zapcore.DPanicLevel: "CRITICAL",
zapcore.PanicLevel: "ALERT",
zapcore.FatalLevel: "EMERGENCY",
}
var EncoderConfig = zapcore.EncoderConfig{
TimeKey: "eventTime",
LevelKey: "severity",
NameKey: "logger",
CallerKey: "caller",
MessageKey: "message",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: EncodeLevel,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
type Core struct {
zapcore.Core
SetReportLocation bool
ctx *Context
}
func (c *Core) With(fields []zapcore.Field) zapcore.Core {
fields, ctx := c.extractCtx(fields)
return &Core{
Core: c.Core.With(fields),
SetReportLocation: c.SetReportLocation,
ctx: ctx,
}
}
func (c *Core) Check(entry zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
if c.Enabled(entry.Level) {
return ce.AddCore(entry, c)
}
return ce
}
func (c *Core) Write(entry zapcore.Entry, fields []zapcore.Field) error {
loc := c.getReportLocationFromEntry(entry)
if loc != nil {
fields = append(fields, LogReportLocation(loc))
}
fields, ctx := c.extractCtx(fields)
fields = append(fields, zap.Object("context", ctx))
return c.Core.Write(entry, fields)
}
func (c *Core) extractCtx(fields []zapcore.Field) ([]zapcore.Field, *Context) {
output := []zapcore.Field{}
ctx := c.cloneCtx()
for _, f := range fields {
switch f.Key {
case logKeyContextHTTPRequest:
ctx.HTTPRequest = f.Interface.(*HTTPRequest)
case logKeyContextReportLocation:
ctx.ReportLocation = f.Interface.(*ReportLocation)
case logKeyContextUser:
ctx.User = f.String
default:
output = append(output, f)
}
}
return output, ctx
}
func (c *Core) cloneCtx() *Context {
if c.ctx == nil {
return &Context{}
}
return c.ctx.Clone()
}
func (c *Core) getReportLocationFromEntry(entry zapcore.Entry) *ReportLocation {
if !c.SetReportLocation {
return nil
}
caller := entry.Caller
if !caller.Defined {
return nil
}
loc := &ReportLocation{
FilePath: caller.File,
LineNumber: caller.Line,
}
if fn := runtime.FuncForPC(caller.PC); fn != nil {
loc.FunctionName = fn.Name()
}
return loc
}
func LogServiceContext(ctx *ServiceContext) zapcore.Field {
return zap.Object(logKeyServiceContext, ctx)
}
func LogHTTPRequest(req *HTTPRequest) zapcore.Field {
return zap.Object(logKeyContextHTTPRequest, req)
}
func LogUser(user string) zapcore.Field {
return zap.String(logKeyContextUser, user)
}
func LogReportLocation(loc *ReportLocation) zapcore.Field {
return zap.Object(logKeyContextReportLocation, loc)
}
func EncodeLevel(lv zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(logLevelSeverity[lv])
}