forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 9
/
selective.go
66 lines (57 loc) · 1.84 KB
/
selective.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
package logp
import (
"go.uber.org/zap/zapcore"
)
type selectiveCore struct {
allSelectors bool
selectors map[string]struct{}
core zapcore.Core
}
func selectiveWrapper(core zapcore.Core, selectors map[string]struct{}) zapcore.Core {
if len(selectors) == 0 {
return core
}
_, allSelectors := selectors["*"]
return &selectiveCore{selectors: selectors, core: core, allSelectors: allSelectors}
}
// Enabled returns whether a given logging level is enabled when logging a
// message.
func (c *selectiveCore) Enabled(level zapcore.Level) bool {
return c.core.Enabled(level)
}
// With adds structured context to the Core.
func (c *selectiveCore) With(fields []zapcore.Field) zapcore.Core {
return selectiveWrapper(c.core.With(fields), c.selectors)
}
// Check determines whether the supplied Entry should be logged (using the
// embedded LevelEnabler and possibly some extra logic). If the entry
// should be logged, the Core adds itself to the CheckedEntry and returns
// the result.
//
// Callers must use Check before calling Write.
func (c *selectiveCore) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
if c.Enabled(ent.Level) {
if ent.Level == zapcore.DebugLevel {
if c.allSelectors {
return ce.AddCore(ent, c)
} else if _, enabled := c.selectors[ent.LoggerName]; enabled {
return ce.AddCore(ent, c)
}
return ce
}
return ce.AddCore(ent, c)
}
return ce
}
// Write serializes the Entry and any Fields supplied at the log site and
// writes them to their destination.
//
// If called, Write should always log the Entry and Fields; it should not
// replicate the logic of Check.
func (c *selectiveCore) Write(ent zapcore.Entry, fields []zapcore.Field) error {
return c.core.Write(ent, fields)
}
// Sync flushes buffered logs (if any).
func (c *selectiveCore) Sync() error {
return c.core.Sync()
}