Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand LevelEnabler interface by method Level() Level #1143

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions level.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ type LevelEnablerFunc func(zapcore.Level) bool
// Enabled calls the wrapped function.
func (f LevelEnablerFunc) Enabled(lvl zapcore.Level) bool { return f(lvl) }

// Level returns default level
func (f LevelEnablerFunc) Level() zapcore.Level { return zapcore.DebugLevel }

// An AtomicLevel is an atomically changeable, dynamic logging level. It lets
// you safely change the log level of a tree of loggers (the root logger and
// any children created by adding context) at runtime.
Expand Down
1 change: 1 addition & 0 deletions zapcore/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type nopCore struct{}
// NewNopCore returns a no-op Core.
func NewNopCore() Core { return nopCore{} }
func (nopCore) Enabled(Level) bool { return false }
func (nopCore) Level() Level { return DebugLevel }
func (n nopCore) With([]Field) Core { return n }
func (nopCore) Check(_ Entry, ce *CheckedEntry) *CheckedEntry { return ce }
func (nopCore) Write(Entry, []Field) error { return nil }
Expand Down
4 changes: 4 additions & 0 deletions zapcore/increase_level.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func (c *levelFilterCore) Enabled(lvl Level) bool {
return c.level.Enabled(lvl)
}

func (c *levelFilterCore) Level() Level {
return c.core.Level()
}

func (c *levelFilterCore) With(fields []Field) Core {
return &levelFilterCore{c.core.With(fields), c.level}
}
Expand Down
6 changes: 6 additions & 0 deletions zapcore/level.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ func (l Level) Enabled(lvl Level) bool {
return lvl >= l
}

// Level returns true if the given level is at or above this level.
func (l Level) Level() Level {
return l
}

// LevelEnabler decides whether a given logging level is enabled when logging a
// message.
//
Expand All @@ -184,4 +189,5 @@ func (l Level) Enabled(lvl Level) bool {
// FatalLevel, but return false for InfoLevel and DebugLevel.
type LevelEnabler interface {
Enabled(Level) bool
Level() Level
}
1 change: 1 addition & 0 deletions zapcore/sampler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ func (c *countingCore) Write(Entry, []Field) error {

func (c *countingCore) With([]Field) Core { return c }
func (*countingCore) Enabled(Level) bool { return true }
func (*countingCore) Level() Level { return DebugLevel }
func (*countingCore) Sync() error { return nil }

func TestSamplerConcurrent(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions zapcore/tee.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ func (mc multiCore) Enabled(lvl Level) bool {
return false
}

func (mc multiCore) Level() Level {
if len(mc) != 0 {
return mc[0].Level()
}
return DebugLevel
}

func (mc multiCore) Check(ent Entry, ce *CheckedEntry) *CheckedEntry {
for i := range mc {
ce = mc[i].Check(ent, ce)
Expand Down
1 change: 1 addition & 0 deletions zapio/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ func BenchmarkWriter(b *testing.B) {
type partiallyNopCore struct{}

func (*partiallyNopCore) Enabled(zapcore.Level) bool { return true }
func (*partiallyNopCore) Level() zapcore.Level { return zapcore.DebugLevel }

func (c *partiallyNopCore) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
return ce.AddCore(ent, c)
Expand Down