-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.go
198 lines (172 loc) · 4.33 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package zapstackdriver
import (
"errors"
"cloud.google.com/go/logging"
"github.com/google/uuid"
"go.uber.org/zap/zapcore"
structpb "google.golang.org/protobuf/types/known/structpb"
logpb "cloud.google.com/go/logging/apiv2/loggingpb"
)
// Core is the core implements zapcore.Core
type Core struct {
zapcore.LevelEnabler
clogger *logging.Logger
encoder *StructEncoder
}
// CoreOptionFunc -
type CoreOptionFunc func(*Core) error
// New -
func New(enab zapcore.LevelEnabler, cloudLogger *logging.Logger, options ...CoreOptionFunc) (*Core, error) {
c := &Core{
LevelEnabler: enab,
clogger: cloudLogger,
encoder: NewStructEncoder(),
}
if c.clogger == nil {
return nil, errors.New("Cloud Logger is required")
}
// Run the options on it
for _, option := range options {
if err := option(c); err != nil {
return nil, err
}
}
return c, nil
}
// With -
func (c *Core) With(fields []zapcore.Field) zapcore.Core {
clone := c.clone()
addFields(clone.encoder, fields)
return clone
}
// Check -
func (c *Core) Check(entry zapcore.Entry, checked *zapcore.CheckedEntry) *zapcore.CheckedEntry {
if c.Enabled(entry.Level) {
return checked.AddCore(entry, c)
}
return checked
}
// Write -
func (c *Core) Write(ent zapcore.Entry, fields []zapcore.Field) error {
e2, err := c.encoder.encodeEntry(ent, fields)
if err != nil {
return err
}
if ent.Stack != "" {
e2.AddString("stack", ent.Stack)
// Add the context for error reporting if this is an error level message
if ent.Level >= zapcore.ErrorLevel {
addContext(e2, ent)
addServiceContext(e2)
}
}
e2.AddString("message", ent.Message)
entry := logging.Entry{
Timestamp: ent.Time,
Severity: zapLevelToSeverity(ent.Level),
InsertID: uuid.New().String(),
Payload: e2.Struct,
}
if e2.req != nil {
entry.HTTPRequest = &logging.HTTPRequest{
Request: e2.req,
}
}
if ent.Caller.Defined {
// e2.AddString("caller", ent.Caller.String())
entry.SourceLocation = &logpb.LogEntrySourceLocation{
File: ent.Caller.File,
Line: int64(ent.Caller.Line),
Function: ent.Caller.TrimmedPath(),
}
}
c.clogger.Log(entry)
return nil
}
// Sync - call stackdriver logger to 'Flush'
func (c *Core) Sync() error {
return c.clogger.Flush()
}
// addContext - Add's context to the encoder
func addContext(e2 *StructEncoder, ent zapcore.Entry) {
reportLocation := map[string]*structpb.Value{}
// If caller is defined, add the file, line & function
if ent.Caller.Defined {
contextFields := map[string]*structpb.Value{}
contextFields["filePath"] = &structpb.Value{
Kind: &structpb.Value_StringValue{
StringValue: ent.Caller.File,
},
}
contextFields["lineNumber"] = &structpb.Value{
Kind: &structpb.Value_NumberValue{
NumberValue: float64(ent.Caller.Line),
},
}
contextFields["functionName"] = &structpb.Value{
Kind: &structpb.Value_StringValue{
StringValue: ent.Caller.Function,
},
}
// Add fields to reportLocation
reportLocation["reportLocation"] = &structpb.Value{
Kind: &structpb.Value_StructValue{
StructValue: &structpb.Struct{
Fields: contextFields,
},
},
}
}
// Add it to context
e2.Struct.Fields["context"] = &structpb.Value{
Kind: &structpb.Value_StructValue{
StructValue: &structpb.Struct{
Fields: reportLocation,
},
},
}
}
// addServiceContext - Add's service context to the encoder
func addServiceContext(e2 *StructEncoder) {
contextFields := map[string]*structpb.Value{}
contextFields["service"] = &structpb.Value{
Kind: &structpb.Value_StringValue{
StringValue: "GO",
},
}
// Add it to context
e2.Struct.Fields["serviceContext"] = &structpb.Value{
Kind: &structpb.Value_StructValue{
StructValue: &structpb.Struct{
Fields: contextFields,
},
},
}
}
func (c *Core) clone() *Core {
return &Core{
LevelEnabler: c.LevelEnabler,
encoder: c.encoder.clone(),
clogger: c.clogger,
}
}
func zapLevelToSeverity(level zapcore.Level) logging.Severity {
switch level {
case zapcore.DebugLevel:
return logging.Debug
case zapcore.InfoLevel:
return logging.Info
case zapcore.WarnLevel:
return logging.Warning
case zapcore.ErrorLevel:
return logging.Error
case zapcore.DPanicLevel:
return logging.Critical
case zapcore.PanicLevel:
return logging.Alert
case zapcore.FatalLevel:
return logging.Emergency
default:
return logging.Info
}
}