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

Fix IncreaseLevel being reset after With #812

Merged
merged 2 commits into from
Apr 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 11 additions & 2 deletions increase_level_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ import (
"go.uber.org/zap/zaptest/observer"
)

func newLoggedEntry(level zapcore.Level, msg string) observer.LoggedEntry {
func newLoggedEntry(level zapcore.Level, msg string, fields ...zapcore.Field) observer.LoggedEntry {
if len(fields) == 0 {
fields = []zapcore.Field{}
}
return observer.LoggedEntry{
Entry: zapcore.Entry{Level: level, Message: msg},
Context: []zapcore.Field{},
Context: fields,
}
}

Expand Down Expand Up @@ -75,9 +78,15 @@ func TestIncreaseLevel(t *testing.T) {
errorLogger.Warn("ignored warn log")
errorLogger.Error("increase level error log")

withFields := errorLogger.With(String("k", "v"))
withFields.Debug("ignored debug log with fields")
withFields.Warn("ignored warn log with fields")
withFields.Error("increase level error log with fields")

assert.Equal(t, []observer.LoggedEntry{
newLoggedEntry(WarnLevel, "original warn log"),
newLoggedEntry(ErrorLevel, "increase level error log"),
newLoggedEntry(ErrorLevel, "increase level error log with fields", String("k", "v")),
}, logs.AllUntimed(), "unexpected logs")

assert.Empty(t, errorOut.String(), "expect no error output")
Expand Down
17 changes: 14 additions & 3 deletions zapcore/increase_level.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ package zapcore
import "fmt"

type levelFilterCore struct {
Core

core Core
level LevelEnabler
}

Expand All @@ -46,10 +45,22 @@ func (c *levelFilterCore) Enabled(lvl Level) bool {
return c.level.Enabled(lvl)
}

func (c *levelFilterCore) With(fields []Field) Core {
return &levelFilterCore{c.core.With(fields), c.level}
}

func (c *levelFilterCore) Check(ent Entry, ce *CheckedEntry) *CheckedEntry {
if !c.Enabled(ent.Level) {
return ce
}

return c.Core.Check(ent, ce)
return c.core.Check(ent, ce)
}

func (c *levelFilterCore) Write(ent Entry, fields []Field) error {
return c.core.Write(ent, fields)
}

func (c *levelFilterCore) Sync() error {
return c.core.Sync()
}
11 changes: 11 additions & 0 deletions zapcore/increase_level_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
. "go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
)
Expand All @@ -35,6 +36,7 @@ func TestIncreaseLevel(t *testing.T) {
coreLevel Level
increaseLevel Level
wantErr bool
with []Field
}{
{
coreLevel: InfoLevel,
Expand All @@ -49,6 +51,11 @@ func TestIncreaseLevel(t *testing.T) {
coreLevel: InfoLevel,
increaseLevel: ErrorLevel,
},
{
coreLevel: InfoLevel,
increaseLevel: ErrorLevel,
with: []Field{zap.String("k", "v")},
},
{
coreLevel: ErrorLevel,
increaseLevel: DebugLevel,
Expand Down Expand Up @@ -82,6 +89,10 @@ func TestIncreaseLevel(t *testing.T) {
return
}

if len(tt.with) > 0 {
filteredLogger = filteredLogger.With(tt.with)
}

require.NoError(t, err)

for l := DebugLevel; l <= FatalLevel; l++ {
Expand Down