/
uniqualizer.go
61 lines (51 loc) · 1.41 KB
/
uniqualizer.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
package zaputils
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// This is a wrapper core that makes sure that pre-specified fields are unique
type uniqueFieldsCore struct{
root zapcore.Core
current zapcore.Core
fields []zapcore.Field
}
func MakeFieldsUnique() zap.Option {
return zap.WrapCore(func(core zapcore.Core) zapcore.Core {
return &uniqueFieldsCore{
root: core,
current: core,
}
})
}
func (u uniqueFieldsCore) Enabled(level zapcore.Level) bool {
return u.current.Enabled(level)
}
func (u uniqueFieldsCore) With(newFields []zapcore.Field) zapcore.Core {
// Copy fields
newFieldList := make([]zapcore.Field, 0, len(u.fields)+len(newFields))
outer: for _, f := range u.fields {
// Skip all the existing fields with the names that match one
// of the new fields.
for _, k := range newFields {
if f.Key == k.Key {
continue outer
}
}
newFieldList = append(newFieldList, f)
}
newFieldList = append(newFieldList, newFields...)
return &uniqueFieldsCore{
root: u.root,
current: u.root.With(newFieldList),
fields: newFieldList,
}
}
func (u uniqueFieldsCore) Check(entry zapcore.Entry, checked *zapcore.CheckedEntry) *zapcore.CheckedEntry {
return u.current.Check(entry, checked)
}
func (u uniqueFieldsCore) Write(entry zapcore.Entry, fields []zapcore.Field) error {
return u.current.Write(entry, fields)
}
func (u uniqueFieldsCore) Sync() error {
return u.current.Sync()
}