/
logger.go
211 lines (177 loc) · 5.32 KB
/
logger.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
199
200
201
202
203
204
205
206
207
208
209
210
211
package logger
import (
"encoding/json"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zapgrpc"
"google.golang.org/grpc/grpclog"
)
var logger = zap.NewNop()
var sugar = logger.Sugar()
type Field struct {
Key string
Value interface{}
}
func parse(fields ...Field) []zapcore.Field {
pFields := []zapcore.Field{}
for i := 0; i < len(fields); i++ {
key := fields[i].Key
value := fields[i].Value
switch t := value.(type) {
case string:
pFields = append(pFields, zapcore.Field{
Key: key,
String: t,
Type: zapcore.StringType,
})
case []byte:
pFields = append(pFields, zapcore.Field{
Key: key,
String: string(t),
Type: zapcore.StringType,
})
case interface{}:
b, err := json.Marshal(t)
if err != nil {
eStruct := map[string]interface{}{
"err": err.Error(),
"msg": "unable to parse",
}
eB, _ := json.Marshal(eStruct)
pFields = append(pFields, zapcore.Field{
Key: key,
String: string(eB),
Type: zapcore.StringType,
})
continue
}
pFields = append(pFields, zapcore.Field{
Key: key,
String: string(b),
Type: zapcore.StringType,
})
}
}
return pFields
}
func formatDate(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
t = t.UTC()
year, month, day := t.Date()
hour, minute, second := t.Clock()
micros := t.Nanosecond() / 1000
buf := make([]byte, 27)
buf[0] = byte((year/1000)%10) + '0'
buf[1] = byte((year/100)%10) + '0'
buf[2] = byte((year/10)%10) + '0'
buf[3] = byte(year%10) + '0'
buf[4] = '-'
buf[5] = byte((month)/10) + '0'
buf[6] = byte((month)%10) + '0'
buf[7] = '-'
buf[8] = byte((day)/10) + '0'
buf[9] = byte((day)%10) + '0'
buf[10] = 'T'
buf[11] = byte((hour)/10) + '0'
buf[12] = byte((hour)%10) + '0'
buf[13] = ':'
buf[14] = byte((minute)/10) + '0'
buf[15] = byte((minute)%10) + '0'
buf[16] = ':'
buf[17] = byte((second)/10) + '0'
buf[18] = byte((second)%10) + '0'
buf[19] = '.'
buf[20] = byte((micros/100000)%10) + '0'
buf[21] = byte((micros/10000)%10) + '0'
buf[22] = byte((micros/1000)%10) + '0'
buf[23] = byte((micros/100)%10) + '0'
buf[24] = byte((micros/10)%10) + '0'
buf[25] = byte((micros)%10) + '0'
buf[26] = 'Z'
enc.AppendString(string(buf))
}
// Configure initializes Istio's logging subsystem.
//
// You typically call this once at process startup.
// Once this call returns, the logging system is ready to accept data.
func Configure(config ...Config) error {
cfg := configDefault(config...)
outputLevel, err := cfg.GetOutputLevel()
if err != nil {
// bad format specified
return err
}
stackTraceLevel, err := cfg.GetStackTraceLevel()
if err != nil {
// bad format specified
return err
}
encCfg := zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "level",
CallerKey: "caller",
MessageKey: "message",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeTime: formatDate,
}
enc := zapcore.NewJSONEncoder(encCfg)
opts := []zap.Option{
zap.ErrorOutput(zapcore.AddSync(cfg.ErrOutput)),
}
if cfg.IncludeCallerSourceLocation {
opts = append(opts, zap.AddCaller())
}
if stackTraceLevel != NoneLevel {
opts = append(opts, zap.AddStacktrace(levelToZap[stackTraceLevel]))
}
l := zap.New(
zapcore.NewCore(enc, zapcore.AddSync(cfg.Output), zap.NewAtomicLevelAt(levelToZap[outputLevel])),
opts...,
)
logger = l.WithOptions(zap.AddCallerSkip(1), zap.AddStacktrace(levelToZap[stackTraceLevel]))
sugar = logger.Sugar()
// capture global zap logging and force it through our logger
_ = zap.ReplaceGlobals(l)
// capture standard golang "log" package output and force it through our logger
_ = zap.RedirectStdLog(logger)
// capture gRPC logging
if cfg.LogGrpc {
grpclog.SetLoggerV2(zapgrpc.NewLogger(logger.WithOptions(zap.AddCallerSkip(2))))
}
return nil
}
// Debug outputs a message at debug level.
// This call is a wrapper around [Logger.Debug](https://godoc.org/go.uber.org/zap#Logger.Debug)
func Debug(args ...interface{}) {
sugar.Debug(args...)
}
// Error outputs a message at error level.
// This call is a wrapper around [logger.Error](https://godoc.org/go.uber.org/zap#logger.Error)
func Error(msg string, fields ...Field) {
logger.Error(msg, parse(fields...)...)
}
// Warn outputs a message at warn level.
// This call is a wrapper around [logger.Warn](https://godoc.org/go.uber.org/zap#logger.Warn)
func Warn(msg string, fields ...Field) {
logger.Warn(msg, parse(fields...)...)
}
// Info outputs a message at information level.
// This call is a wrapper around [logger.Info](https://godoc.org/go.uber.org/zap#logger.Info)
func Info(msg string, fields ...Field) {
logger.Info(msg, parse(fields...)...)
}
// With creates a child logger and adds structured context to it. Fields added
// to the child don't affect the parent, and vice versa.
// This call is a wrapper around [logger.With](https://godoc.org/go.uber.org/zap#logger.With)
func With(fields ...zapcore.Field) *zap.Logger {
return logger.With(fields...)
}
// Sync flushes any buffered log entries.
// Processes should normally take care to call Sync before exiting.
// This call is a wrapper around [logger.Sync](https://godoc.org/go.uber.org/zap#logger.Sync)
func Sync() error {
return logger.Sync()
}