-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfns.go
206 lines (183 loc) · 4.69 KB
/
fns.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
package log
import (
"fmt"
"io"
"os"
"strings"
"github.com/cihub/seelog"
"go.uber.org/multierr"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
_multipleErrMsg = "Multiple errors without a key."
)
var (
nope = func(string, ...interface{}) {}
nopeLevel = func(string, ...interface{}) {}
toWriter = func(w io.Writer, msg string, a ...interface{}) {
var b strings.Builder
b.WriteString(fmt.Sprintf(msg, a...))
b.WriteString("\n")
w.Write([]byte(b.String()))
}
toDiscard = func(msg string, a ...interface{}) { toWriter(io.Discard, msg, a...) }
toStderr = func(msg string, a ...interface{}) { toWriter(os.Stderr, msg, a...) }
toStdout = func(msg string, a ...interface{}) { toWriter(os.Stdout, msg, a...) }
)
type invalidPair struct {
position int
key, value interface{}
}
func LogFn(any logger, l Level) func(string, ...interface{}) {
switch lgr := any.(type) {
case seelog.LoggerInterface:
return func(msg string, a ...interface{}) {
switch l {
case Quiet:
case Trace:
lgr.Tracef(msg, a...)
case Debug:
lgr.Debugf(msg, a...)
case Info:
lgr.Infof(msg, a...)
case Warn:
lgr.Warnf(msg, a...)
case Error:
lgr.Errorf(msg, a...)
case Critical:
lgr.Criticalf(msg, a...)
}
lgr.Flush()
}
case *zap.SugaredLogger:
return func(msg string, a ...interface{}) {
isFirstZapField := false
if len(a) > 0 {
_, isFirstZapField = a[0].(zap.Field)
}
if isFirstZapField {
switch l {
case Quiet:
case Trace, Debug:
lgr.Debugw(msg, a...)
case Info:
lgr.Infow(msg, a...)
case Warn:
lgr.Warnw(msg, a...)
case Error:
lgr.Errorw(msg, a...)
case Critical:
lgr.Fatalw(msg, a...)
}
} else {
switch l {
case Quiet:
case Trace, Debug:
lgr.Debugf(msg, a...)
case Info:
lgr.Infof(msg, a...)
case Warn:
lgr.Warnf(msg, a...)
case Error:
lgr.Errorf(msg, a...)
case Critical:
lgr.Fatalf(msg, a...)
}
}
}
case *zap.Logger:
return func(msg string, a ...interface{}) {
var param []zap.Field
if len(a) != 0 {
param = make([]zap.Field, 0, len(a))
var (
invalid invalidPairs
seenError bool
)
for i := 0; i < len(a); {
// This is a strongly-typed field. Consume it and move on.
if f, ok := a[i].(zap.Field); ok {
param = append(param, f)
i++
continue
}
// If it is an error, consume it and move on.
if err, ok := a[i].(error); ok {
if !seenError {
seenError = true
param = append(param, zap.Error(err))
} else {
lgr.With().Error(_multipleErrMsg, zap.Error(err))
}
i++
continue
}
// Make sure this element isn't a dangling key.
if i == len(a)-1 {
param = append(param, zap.Any("ignored", a[i]))
break
}
// Consume this value and the next, treating them as a key-value pair. If the
// key isn't a string, add this pair to the slice of invalid pairs.
key, val := a[i], a[i+1]
if keyStr, ok := key.(string); !ok {
// Subsequent errors are likely, so allocate once up front.
if cap(invalid) == 0 {
invalid = make(invalidPairs, 0, len(a)/2)
}
invalid = append(invalid, invalidPair{i, key, val})
} else {
param = append(param, zap.Any(keyStr, val))
}
i += 2
}
// If we encountered any invalid key-value pairs, log an error.
if len(invalid) > 0 {
param = append(param, zap.Array("invalid", invalid))
}
}
switch l {
case Quiet:
case Trace, Debug:
lgr.Debug(msg, param...)
case Info:
lgr.Info(msg, param...)
case Warn:
lgr.Warn(msg, param...)
case Error:
lgr.Error(msg, param...)
case Critical:
lgr.Fatal(msg, param...)
}
}
}
switch l {
case Quiet:
return toDiscard
case Trace, Debug, Info:
return toStdout
case Warn, Error, Critical:
return toStderr
}
return nope
}
func Log(any logger, l Level, msg string) { LogFn(any, l)(msg) }
func LogToDiscard(msg string, a ...interface{}) { LogFn(nil, Quiet)(msg, a...) }
func LogStd(l Level, msg string, a ...interface{}) { LogFn(nil, l)(msg, a...) }
func Logf(any logger, l Level, msg string, a ...interface{}) { LogFn(any, l)(msg, a...) }
func LogfStd(l Level, msg string, a ...interface{}) { LogStd(l, msg, a...) }
type invalidPairs []invalidPair
func (ps invalidPairs) MarshalLogArray(enc zapcore.ArrayEncoder) error {
var err error
for i := range ps {
err = multierr.Append(err, enc.AppendObject(ps[i]))
}
return err
}
func (p invalidPair) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddInt64("position", int64(p.position))
zap.Any("key", p.key).AddTo(enc)
zap.Any("value", p.value).AddTo(enc)
return nil
}