/
filter_level.go
37 lines (30 loc) · 1.11 KB
/
filter_level.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
package logging
import "go.uber.org/zap/zapcore"
// FilterLevel wraps an existing zap.Core but logs at a different level.
// This function can be used to create child loggers which should print
// only log levels at a different log level than the parent logger.
func FilterLevel(level zapcore.Level) func(zapcore.Core) zapcore.Core {
return func(c zapcore.Core) zapcore.Core {
return newLevelFilterCore(c, level)
}
}
// levelFilterCore allows to change the log level on the fly. This must remain here until
// https://github.com/uber-go/zap/pull/775 is merged and released
type levelFilterCore struct {
zapcore.Core
level zapcore.Level
}
func newLevelFilterCore(core zapcore.Core, level zapcore.Level) zapcore.Core {
return &levelFilterCore{core, level}
}
// Enabled checks if the lvl is to be printed
func (c *levelFilterCore) Enabled(lvl zapcore.Level) bool {
return lvl >= c.level
}
// Check determines whether the supplied Entry should be logged
func (c *levelFilterCore) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
if !c.Enabled(ent.Level) {
return ce
}
return c.Core.Check(ent, ce)
}